[英]Restassured test error - No Content-Type was specified in response
I try to test my rest endpoint with restassured.我尝试放心地测试我的休息端点。 The response is always a html document, altough i set the accept-header to "application/json".响应始终是一个 html 文档,尽管我将 accept-header 设置为“application/json”。
*java.lang.IllegalStateException: Cannot parse object because no supported Content-Type was specified in response. *java.lang.IllegalStateException:无法解析对象,因为在响应中没有指定支持的内容类型。 Content-Type was 'text/html;charset=utf-8'.内容类型为“text/html;charset=utf-8”。
[main] DEBUG org.apache.http.wire - << "HTTP Status 400 [0xe2][0x80][0x93] Bad Request* [main] DEBUG org.apache.http.wire - << "HTTP 状态 400 [0xe2][0x80][0x93] 错误请求*
edit: In postman its working with the same request.编辑:在邮递员中,它处理相同的请求。
myClass result = given()
.contentType("application/json")
.accept("application/json")
.header("sessionid", sessionId)
.body(myBody)
.when()
.post(getInternalEndpoint() + "/rest/v1/myEndpoint").as(myClass.class);
I think you might have not properly sent the request.我认为您可能没有正确发送请求。 I have written the below in Java using Rest Assured:我使用 Rest Assured 在 Java 中编写了以下代码:
RequestSpecification httprequest = RestAssured.given();
//request Payload
JSONObject js =new JSONObject();
js.put("name","xyz");
//Add a header stating that request body is a JSON
httprequest.header("Content-Type","application/json");
httprequest.body(js.toJSONString());
httprequest.header("Authorization","Bearer yourAPIKEy");
httprequest.log().all();
//Response
Response response = httprequest.request(Method.POST,"/rest/v1/myEndpoint");
String responseBody = response.asString();
//JsonPath to read response body
JsonPath json=response.jsonPath();
String statusCode = json.get("statusCode)).toString();
System.out.println(statusCode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.