繁体   English   中英

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

[英]Restassured test error - No Content-Type was specified in response

我尝试放心地测试我的休息端点。 响应始终是一个 html 文档,尽管我将 accept-header 设置为“application/json”。

*java.lang.IllegalStateException:无法解析对象,因为在响应中没有指定支持的内容类型。 内容类型为“text/html;charset=utf-8”。

[main] DEBUG org.apache.http.wire - << "HTTP 状态 400 [0xe2][0x80][0x93] 错误请求*

编辑:在邮递员中,它处理相同的请求。

myClass result = given()
                    .contentType("application/json")
                    .accept("application/json")
                    .header("sessionid", sessionId)
                    .body(myBody)
                    .when()
                    .post(getInternalEndpoint() + "/rest/v1/myEndpoint").as(myClass.class);

我认为您可能没有正确发送请求。 我使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM