【问题标题】:Response body of a Post Request can't be displayed无法显示发布请求的响应正文
【发布时间】:2020-10-03 15:11:12
【问题描述】:

我想知道是否有人可以提供帮助。我正在尝试发送一个发布请求来创建一个项目,并且我得到了正确的状态代码,但没有得到响应正文。我对 API 相当陌生,所以对此有点困惑。谷歌搜索也没有帮助。这是我的代码:-

public void createGoods() {
    RequestSpecification httpRequest = RestAssured.given();
    RestAssured.baseURI = "http://192.168.0.31:8081/transit-api/clothes?_dc=159053130885&productId=1";
    JSONObject requestParams = new JSONObject();
    requestParams.put("productId", "1");
    requestParams.put("productDeploymentId", "1");
    requestParams.put("displayableId", goodsDisplayable);
    requestParams.put("createdUserName", "null");
    requestParams.put("product", "null");
    requestParams.put("productDescription", "");
    requestParams.put("createdDate", "null");
    requestParams.put("lastEventDate", "null");
    requestParams.put("progressDescription", "");
    requestParams.put("progressDate", "null");
    requestParams.put("active", true);
    requestParams.put("treatmentMilestones", "null");
    requestParams.put("status", "null");
    requestParams.put("stopComment", "");
    requestParams.put("primaryIdentifierString", "");
    requestParams.put("isPrimaryIdentifierAvailable", false);
    httpRequest
            .header("Accept", "application/json")
            .header("Accept-Language", "en-US,en;q=0.9")
            .header("Connection", "keep-alive")
            .header("Accept-Encoding", "gzip, deflate, br")
            .contentType(ContentType.JSON)
            .header("X-Requested-With", "XMLHttpRequest")
            .sessionId(sessionId);

    httpRequest.body(requestParams.toJSONString());
    Response response = httpRequest.request(Method.POST);

    System.out.println("RESPONSE body is--->>>>" + response.getBody().asString());
    System.out.println("Status code is----------------->>>" + response.getStatusCode());

}

}

我得到的回复如下:-

>>>>

RESPONSE body is---createTreatment----------->>>>*io.restassured.internal.RestAssuredResponseImpl@55259aa7*

Status code is ----------------->>>200

Step failed
java.lang.IllegalStateException: Cannot invoke the path method because no content-type was present in the response and no default parser has been set.
You can specify a default parser using e.g.:
RestAssured.defaultParser = Parser.JSON;

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl$_path_closure5.doCall(RestAssuredResponseOptionsGroovyImpl.groovy:392)

【问题讨论】:

标签: java json content-type rest-assured


【解决方案1】:

解析是一种从响应中读取数据的方法。将以下设置为您的默认解析器。

RestAssured.defaultParser = Parser.JSON;

result read!

【讨论】:

  • 感谢您的回复。当我从 Java 中的方法发布它时,我注意到它的响应标头中没有 Content-Type。但是,当我从 Postman 发帖时,相同的请求在其响应中确实有 Content-Type 并且它工作正常。我也尝试添加解析器,但它仍然有效。从方法:-HEADERS----->Server=Apache-Coyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content-Length=0 日期=Sun, 14 Jun 2020 13:39 :34 GMT。
【解决方案2】:

尝试为 REST Assured 设置默认解析器:

RestAssured.defaultParser = Parser.JSON;

在此处查看示例:https://github.com/rest-assured/rest-assured/blob/rest-assured-4.3.0/examples/rest-assured-itest-java/src/test/java/io/restassured/itest/java/DefaultParserITest.java#L44-L52

或者,确保您正在测试的服务返回有效的Content-Type HTTP 响应标头。

【讨论】:

  • 感谢您的回复。当我从 Java 中的方法发布它时,我注意到它的响应标头中没有 Content-Type。但是,当我从 Postman 发帖时,相同的请求在其响应中确实有 Content-Type 并且它工作正常。我也尝试添加解析器,但它仍然有效。从方法:-HEADERS----->Server=Apache-Coyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content-Length=0 日期=Sun, 14 Jun 2020 13:39 :34 GMT。
  • 这暗示了一个不需要Content-Type的空响应正文,请参阅Content-Length HTTP响应标头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2023-02-01
相关资源
最近更新 更多