簡體   English   中英

無法顯示發布請求的響應正文

[英]Response body of a Post Request can't be displayed

我想知道是否有人可以提供幫助。 我正在嘗試發送一個發布請求來創建一個項目,並且我得到了正確的狀態代碼,但沒有得到響應正文。 我對 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)

嘗試為 REST 設置默認解析器

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 響應 header。

解析是一種從響應中讀取數據的方法。 將以下設置為默認解析器。

RestAssured.defaultParser = Parser.JSON;

result read!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM