使用RestArrured触发时,API返回文本/ html而不是JSON

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

我为收集请求触发了Zomato api,但是,我一直在获取标头,就像它们是text / html一样。邮递员以JSON形式返回它们,这就是我需要的。到目前为止,我测试过的所有其他Zomato api都返回JSON,但返回collections并不确定原因。这就是我试图将JSON强制为响应类型的原因。

@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());
}
rest-assured zomato-api
1个回答
0
投票

我将响应转换为JsonPath:

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

然后使用此JsonPath获取Response的每个值:

JsonPath jp = getJsonPath(res);
String type = jp.get("type");
© www.soinside.com 2019 - 2024. All rights reserved.