保证测试错误-没有在响应中指定Content-Type

时间:2019-04-15 07:20:11

标签: rest api testing microservices rest-assured

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

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

[main]调试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);

1 个答案:

答案 0 :(得分:0)

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