简体   繁体   English

使用 RestArrured 触发时 API 返回 text/html 而不是 JSON

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

I'm triggering the Zomato api for the collections request, however, I keep getting the headers as if they were text/html.我正在为集合请求触发 Zomato api,但是,我一直在获取标题,就好像它们是 text/html 一样。 Postman returns them as JSON, which is what I need.邮递员将它们作为 JSON 返回,这正是我所需要的。 All other Zomato apis I've tested so far are returning JSON but collections and not sure why.所有其他Zomato的API到目前为止,我测试了正在返回JSON,但收藏品和不知道为什么。 That's what I have on my attempts to force the JSON as the type for the response.这就是我试图将 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());
}

I convert response to JsonPath :我将响应转换为 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 来获取 Response 的每个值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请放心:发布请求返回错误“JSON 输入文本既不应该为 null 也不应该为空。” - Rest Assured:Post request returning an error "The JSON input text should neither be null nor empty." API测试-在对JSON使用restassured时导致方括号中的值导致断言失败 - API Testing - getting value in squarebraces when using restassured for json resulting in assertion failure 在放心的 API 测试中使用 queryParams 而不是 Json 主体 - Using queryParams instead of Json body in Restassured API testing 在 API 测试中尝试 POST JSON object 时出错 - Error when attempting to POST JSON object in API test 使用Rest Assured从JSON API获取唯一属性 - Getting unique attributes from a JSON API using Rest Assured 更新来自 API 的 JSON 响应,并使用 Rest Assured 将更新后的响应作为输入/正文传递给另一个 API - Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured 使用Rest Assured Get类型返回400状态代码的测试REST API - Test REST API using Rest Assured Get type returning 400 status code 如何将JSON响应转换为Java列表-使用Rest Assure确保进行API测试 - How to convert JSON response to Java List- Using Rest Assured for API Testing 再次保证,如何在 api 的 json 响应中使用其兄弟属性值获取特定属性值? - In restassurred, how to get particular attribute value using its sibling attribute value in json response of api? 使用 java 验证 Json 响应 - Verify Json response using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM