简体   繁体   中英

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".

*java.lang.IllegalStateException: Cannot parse object because no supported Content-Type was specified in response. Content-Type was 'text/html;charset=utf-8'.

[main] DEBUG org.apache.http.wire - << "HTTP Status 400 [0xe2][0x80][0x93] Bad Request*

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:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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