放心测试错误-未在响应中指定内容类型

问题描述 投票:0回答:1

我尝试使用安全性测试我的其余端点。响应始终是一个html文档,尽管我将accept-header设置为“ application / json”。

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

[main] DEBUG org.apache.http.wire-<

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

myClass result = given()
                    .contentType("application/json")
                    .accept("application/json")
                    .header("sessionid", sessionId)
                    .body(myBody)
                    .when()
                    .post(getInternalEndpoint() + "/rest/v1/myEndpoint").as(myClass.class);
rest api testing microservices rest-assured
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.