简体   繁体   English

Restassured 测试错误 - No Content-Type was specified in response

[英]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.

相关问题 RESTAssured 多部分内容类型 - RESTAssured Multipart content-type 服务无法识别RestAssured内容类型application / json - RestAssured content-type application/json not being recognised by service 如何使用SOAP UI测试响应内容类型 - How to test the response content-type using SOAP UI 无法解析对象,因为未在响应中指定未支持的Content-Type。 内容类型为&#39;text / html; charset = utf-8&#39; - Cannot parse object because no supported Content-Type was not specified in response. Content-Type was 'text/html;charset=utf-8' REST-API 错误响应的不同内容类型 - REST-API Different Content-Type on Error Response REST Web服务:响应状态为4XX时可接受的HTTP响应内容类型(客户端错误) - REST Web Service: Acceptable HTTP response content-type when responding with status 4XX (Client Error) 内部服务器错误,不支持的内容类型 - Internal Server Error on unsupported content-type 如何更改REST DataSnap中的Content-Type响应 - How to change the Content-Type response in the REST DataSnap 这些REST HTTP响应代码正确吗?内容类型又如何? - Are these REST HTTP response codes right, and what about the Content-Type? 如果响应是加密的 json,那么 Content-Type 应该是什么? - What should be Content-Type if response is encrypted json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM