API returning text/html rather than JSON when triggered using RestArrured

849 views Asked by At

I'm triggering the Zomato api for the collections request, however, I keep getting the headers as if they were text/html. Postman returns them as JSON, which is what I need. All other Zomato apis I've tested so far are returning JSON but collections and not sure why. That's what I have on my attempts to force the JSON as the type for the response.

@Test
public void testGetCousinesApiReturnsItemsInAscendingAlphabeticalOrder() {

    Map<String, Object> map = new HashMap<>();
    map.put("city_id", 61);

    Response r = given()
            .baseUri(baseUrl)
            .basePath("/cousines")
            .queryParams(map)
            .contentType(ContentType.JSON)
            .accept(ContentType.JSON)
            .contentType("application/json\r\n")
            .header("Accept", "application/json").and()
            .header("Content-Type", "application/json")
            .header("user-key", zomatoKey)
            .log().body(false)
            .get();

    System.out.println(r.getContentType());
}
1

There are 1 answers

1
ssharma On

I convert response to JsonPath :

 public static JsonPath getJsonPath (Response res) {
        String json = res.asString();
        System.out.println("returned json: " + json);
        return new JsonPath(json);
    }

and then use this JsonPath to get each value of Response:

JsonPath jp = getJsonPath(res);
String type = jp.get("type");