java - Post请求的响应体无法显示

标签 java json content-type rest-assured

不知道有没有人可以帮忙。我正在尝试发送一个帖子请求来创建一个项目,我得到了正确的状态代码,但没有得到响应正文。我对 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)

最佳答案

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

RestAssured.defaultParser = Parser.JSON;

result read!

关于java - Post请求的响应体无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62367346/

相关文章:

javascript - smartgwt 禁用网格中的客户端过滤

java - 无效的 Lambda 表达式

javascript - 意外的标记 ?在 JSON 中使用 JSON.parse 的 x 位置

json - 解析 'valid' json 时出现意外标记 {

json - 将字符串转换为具有相同名称的 JSON 对象(js 中的某种动态代码需要此)

web-services - 使用 JAX-RS Jersey 2.2 获取带有 Content-Type 和 Accept header 的请求

java - 第一个按钮仅在重新打开应用程序后才着色

java - 如何从 GWT 插件中消除 Eclipse 中的 "The web.xml file does not exist"错误?

google-chrome - 如何以编程方式查找 Chrome 处理的内容类型?

browser - AWS S3 : Force File Download using 'response-content-disposition'