ZOMATO - Eclipse中的INVALID API Key错误,但在POSTMAN响应中是成功的

时间:2018-06-10 15:47:31

标签: java zomato-api

我正在执行ZOMATO API的GET请求,POSTMAN工具返回一个成功的响应,但是当我尝试使用Eclipse时,它会返回以下消息: 403错误 API密钥无效

我在POSTMAN和Eclipse中使用的API密钥是双重检查的。

日食代码如下所述:

public class exampleTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        RestAssured.baseURI = "https://developers.zomato.com";

        Response res = given().
        param("user-key", <api_key>).
        when().
        get("/api/v2.1/categories").
         then().assertThat().statusCode(400).and().contentType(ContentType.JSON).extract().response();

在POSTMAN工具中,我在标题中传递了密钥,而URL和响应是成功的,如下所示:

{
    "categories": [
        {
            "categories": {
                "id": 1,
                "name": "Delivery"
            }
        },
        {
            "categories": {
                "id": 2,
                "name": "Dine-out"
            }
        },
        {
            "categories": {
                "id": 3,
                "name": "Nightlife"
            }
        },
        {
            "categories": {
                "id": 4,
                "name": "Catching-up"
            }
        },
        {
            "categories": {
                "id": 5,
                "name": "Takeaway"
            }
        },
        {
            "categories": {
                "id": 6,
                "name": "Cafes"
            }
        },
        {
            "categories": {
                "id": 7,
                "name": "Daily Menus"
            }
        },
        {
            "categories": {
                "id": 8,
                "name": "Breakfast"
            }
        },
        {
            "categories": {
                "id": 9,
                "name": "Lunch"
            }
        },
        {
            "categories": {
                "id": 10,
                "name": "Dinner"
            }
        },
        {
            "categories": {
                "id": 11,
                "name": "Pubs & Bars"
            }
        },
        {
            "categories": {
                "id": 13,
                "name": "Pocket Friendly Delivery"
            }
        },
        {
            "categories": {
                "id": 14,
                "name": "Clubs & Lounges"
            }
        }
    ]
}

3 个答案:

答案 0 :(得分:0)

您将api密钥作为RestAssured中的查询参数传递。它应该作为标题参数传递。像这样:

.header("user-key", <api_key>)

答案 1 :(得分:0)

我遇到了这个错误,只是尝试一下即可,

使用apikey代替使用用户密钥。

developers.zomato.com/api/v2.1/categories? apikey =您的API密钥

祝你好运。

答案 2 :(得分:0)

对我来说,它通过使用标头而不是标头函数调用来工作。

RequestSpecification requestSpec=RestAssured.given().headers("Content-Type","application/json","user-key",<api-key>);