0

I am trying to invoke a "put" api call with raw body in my code, but i am getting following error

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 69; received: 68
    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
    at org.apache.http.conn.EofSensorInputStream$read.call(Unknown Source)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.convertStreamToByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:470)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:149)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:100)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:208)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.charsetToString(RestAssuredResponseOptionsGroovyImpl.groovy:522)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl$charsetToString$4.callCurrent(Unknown Source)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asString(RestAssuredResponseOptionsGroovyImpl.groovy:183)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asString(RestAssuredResponseOptionsGroovyImpl.groovy:179)
    at io.restassured.internal.RestAssuredResponseOptionsImpl.asString(RestAssuredResponseOptionsImpl.java:234)
    at com.NexusPortalAutomation.Utilities.Java.restAssured.cancelSpa(restAssured.java:70)
    at com.NexusPortalAutomation.Utilities.Java.restAssured.test(restAssured.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Code:

        char q ='"';
    RestAssured.baseURI = "http://localhost:3000/api/v1/spa/cancel/" + spaIndex;
    String rawbody ="{"+q+"SpaCancel"+q+":[{"+q+"CustomerId"+q+":"+q+Customer+q+","+q+"SpaIndex"+q+spaIndex+q+"CancelDate"+q+":"+q+"2020-06-08"+q+","+q+"CancelReason"+q+":"+q+"Test"+q+","+q+"ReasonCode"+q+":"+q+" "+q+"}]}";
    RequestSpecification httpRequest = RestAssured.given().headers("Authorization", "Bearer " + getToken(),
        "Content-Type", ContentType.JSON, "Accept", ContentType.JSON) ;

    Response response = httpRequest.body(rawbody).put();

    String body = response.getBody().asString();
    System.out.println(body);
QuickSilver
  • 3,915
  • 2
  • 13
  • 29
  • This looks very bad, why are you even using a `"` when you can use that as just string – Wilfred Clement Jun 08 '20 at 16:36
  • Sorry i did not got your point? can you give an example – SeleniumIsLove Jun 08 '20 at 17:09
  • It's not the problem with your client code. There's a problem with the server you're calling. It returns header `Content-Length: 69` with response body that is 68 characters length. Take a look at this similar question - https://stackoverflow.com/questions/23152816/premature-end-of-content-length-delimited-message-body-expected. You should check correctness of your app running at http://localhost:3000 – bhusak Jun 08 '20 at 19:32

2 Answers2

0

I tested your code against a test PUT endpoint and it works fine on my machine. I would bet that the error message you see indicate that the response from your http://localhost:3000/api/v1/spa/cancel/ has issues. As indicated by the error the content-length is not matching what is actually returned.

Raipe
  • 786
  • 1
  • 9
  • 22
  • I am getting error at String body = response.getBody().asString(); rest is working fine – SeleniumIsLove Jun 08 '20 at 17:16
  • 1
    Yes exactly. Meaning the response returned is not valid. You are calling `http://localhost:3000/api/v1/spa/cancel/` and it returns invalid response and thus the `getBody()` fails. – Raipe Jun 09 '20 at 05:32
0

I have found the issue, it was incorrect body as there was ":" missing after "SpaIndex"

String rawbody ="{"+q+"SpaCancel"+q+":[{"+q+"CustomerId"+q+":"+q+Customer+q+","+q+"SpaIndex"+q+spaIndex+q+"CancelDate"+q+":"+q+"2020-06-08"+q+","+q+"CancelReason"+q+":"+q+"Test"+q+","+q+"ReasonCode"+q+":"+q+" "+q+"}]}";