Java Post请求的响应主体可以';不能展示
我想知道是否有人能帮忙。我试图发送一个post请求来创建一个项目,我得到了正确的状态代码,但没有得到响应正文。我对API相当陌生,所以对此有点困惑。谷歌搜索也无济于事。这是我的密码:-Java Post请求的响应主体可以';不能展示,java,json,content-type,rest-assured,Java,Json,Content Type,Rest Assured,我想知道是否有人能帮忙。我试图发送一个post请求来创建一个项目,我得到了正确的状态代码,但没有得到响应正文。我对API相当陌生,所以对此有点困惑。谷歌搜索也无济于事。这是我的密码:- public void createGoods() { RequestSpecification httpRequest = RestAssured.given(); RestAssured.baseURI = "http://192.168.0.31:8081/transit-api/cloth
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 Assured设置默认解析器:
RestAssured.defaultParser = Parser.JSON;
请参见此处的示例:
或者,确保您正在测试的服务返回有效的
内容类型HTTP响应头。尝试为REST-Assured设置默认解析器:
RestAssured.defaultParser = Parser.JSON;
请参见此处的示例:
或者,确保正在测试的服务返回有效的内容类型HTTP响应头。解析是从响应读取数据的一种方法。将下面的语法分析器设置为默认语法分析器
RestAssured.defaultParser = Parser.JSON;
result read!
解析是从响应中读取数据的一种方法。将下面的语法分析器设置为默认语法分析器
RestAssured.defaultParser = Parser.JSON;
result read!
这回答了你的问题吗?这回答了你的问题吗?谢谢你的回复。我注意到,当我从Java中的方法发布它时,它的响应头中没有内容类型。但是,当我从Postman发帖时,同一请求的响应中确实有内容类型,并且工作正常。我也尝试过添加parsor,但仍然有效。方法:-HEADERS----->Server=apachecoyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content Length=0 Date=Sun,2020年6月14日13:39:34 GMT。这将提示不需要内容类型的空响应正文,请参阅内容长度HTTP响应标头。感谢您的回复。我注意到,当我从Java中的方法发布它时,它的响应头中没有内容类型。但是,当我从Postman发帖时,同一请求的响应中确实有内容类型,并且工作正常。我也尝试过添加parsor,但仍然有效。方法:-HEADERS----->Server=apachecoyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content Length=0 Date=Sun,2020年6月14日13:39:34 GMT。这将提示不需要内容类型的空响应正文,请参阅内容长度HTTP响应标头。感谢您的回复。我注意到,当我从Java中的方法发布它时,它的响应头中没有内容类型。但是,当我从Postman发帖时,同一请求的响应中确实有内容类型,并且工作正常。我也尝试过添加parsor,但仍然有效。方法:-HEADERS----->Server=apachecoyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content Length=0 Date=Sun,2020年6月14日13:39:34 GMT。感谢您的回复。我注意到,当我从Java中的方法发布它时,它的响应头中没有内容类型。但是,当我从Postman发帖时,同一请求的响应中确实有内容类型,并且工作正常。我也尝试过添加parsor,但仍然有效。方法:-HEADERS----->Server=apachecoyote/1.1 X-Frame-Options=SAMEORIGIN X-Content-Type-Options=nosniff Content Length=0 Date=Sun,2020年6月14日13:39:34 GMT。