【问题标题】:Postman gives right response but restassured returns wrong response邮递员给出了正确的回应,但放心返回错误的回应
【发布时间】:2019-01-23 07:05:56
【问题描述】:

几个月以来我一直在与放心合作。

在从开源 API 获取数据时,观察到当通过 Postman API 进行 GET 调用时返回 200 并接收到有效/预期的数据。

编写以下代码(使用 Rest-Assured 的 Java)从 API 获取相同的数据:

package com.type.GetFuelTypeFromAPI;

import static io.restassured.RestAssured.given;

import java.net.MalformedURLException;
import java.net.URL;
import org.testng.annotations.Test;

import io.restassured.response.Response;

public class SampleGetAPI {

@Test
public void getDetails() throws MalformedURLException {

    Response response=
    given()
        .queryParam("cmd", "getTrims")
        .queryParam("make", "Abarth")
        .queryParam("year", "1955")
        .queryParam("model", "207")
    .when()
        .get(new URL("https://carqueryapi.com/api/0.3/"));

    String responseBody = response.body().asString();
    System.out.println(responseBody);
}
}

代码输出为:

[TestNG] Running:
C:\Users\AaSomvanshi\AppData\Local\Temp\testng-eclipse-1657322415\testng-customsuite.xml

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /api/0.3/
on this server.<br />
</p>
<p>Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

PASSED: getDetails

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

有人可以指导我如何克服这个问题吗?

【问题讨论】:

    标签: java wireshark rest-assured


    【解决方案1】:

    经过大量搜索,按照以下步骤进行:

    使用 Wireshark 捕获邮递员和放心的 API 调用。

    Postman API 调用包含以下值:


    GET /api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207 HTTP/1.1\r\n

    缓存控制:无缓存\r\n

    邮递员令牌:c70faee6-f00c-47b6-9c6a-bb1c4ffe5cf4\r\n

    用户代理:PostmanRuntime/7.6.0\r\n

    接受:/\r\n

    cookie:__cfduid=dcce85c35c3e5eb33524b0a1a79b6bf2b1548159374\r\n

    接受编码:gzip,放气\r\n

    引用者:https://carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207\r\n

    主机:www.carqueryapi.com\r\n

    连接:保持活动状态\r\n

    \r\n

    [完整请求 URI:http://www.carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207]

    [HTTP 请求 1/1] [帧内响应:350]


    Rest-Assured API 调用包含以下值:


    GET /api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207 HTTP/1.1\r\n

    接受:/\r\n

    主机:www.carqueryapi.com\r\n

    连接:保持活动状态\r\n

    用户代理:Apache-HttpClient/4.5.3 (Java/1.8.0_171)\r\n

    接受编码:gzip,deflate\r\n

    \r\n

    [完整请求 URI:http://www.carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207]

    [HTTP 请求 1/1]

    [帧内响应:769]


    观察:

    标头 User-Agent 的值不同。 API 阻止了 Apache-HttpClient 的请求,但允许 PostmanRuntime/7.6.0

    更新了代码以使标头 User-Agent 的值为 PostmanRuntime/7.6.0 并且可以正常工作。

    下面是工作代码:

    package com.type.GetFuelTypeFromAPI;
    
    import static io.restassured.RestAssured.given;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import org.testng.annotations.Test;
    
    import io.restassured.response.Response;
    
    public class SampleGetAPI {
    
    @Test
    public void getDetails() throws MalformedURLException {
    
        Response response=
        given()
            .header("User-Agent", "PostmanRuntime/7.6.0")
            .queryParam("cmd", "getTrims")
            .queryParam("make", "Abarth")
            .queryParam("year", "1955")
            .queryParam("model", "207")
        .when()
            .get(new URL("https://carqueryapi.com/api/0.3/"));
    
        String responseBody = response.body().asString();
        System.out.println(responseBody);
    }
    }
    

    【讨论】:

    • 如果你能在不到一秒的时间内自己详细回答,你问问题有什么原因吗?
    • 只是想知道是否有其他方法可以解决此类问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2011-03-27
    • 1970-01-01
    • 2020-09-20
    • 2018-02-21
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多