0
votes

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);
1
Exception message is explaining everything. You're expecting "application/json" in response but actual content-type is "text/html". As a result you can't parse response body. Check if your request is correct - bhusak
my request is working in postman. - Satu
@Satu , Postman is intelligent enough to auto detect the response format. As rest-assured is doesn't have such features, it will throw back Exception. - Vamsi Ravi

1 Answers

0
votes

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