邮递员给出了正确的回应,但放心 returns 错误的回应
Postman gives right response but restassured returns wrong response
几个月来一直安心工作
从开源获取数据时 API 观察到当通过 Postman 进行 GET 调用时 API returns 200 和 valid/expected 收到数据。
写了下面的代码(Java 使用 Rest-Assured)从 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
===============================================
有人可以指导我如何解决这个问题吗?
经过大量搜索后,按照以下步骤操作:
使用 Wireshark 捕获邮递员和 rest-assured API 呼叫。
Postman API 调用包含以下值:
GET /api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207 HTTP/1.1\r\n
cache-control: no-cache\r\n
Postman-Token: c70faee6-f00c-47b6-9c6a-bb1c4ffe5cf4\r\n
User-Agent: PostmanRuntime/7.6.0\r\n
接受:/\r\n
cookie:__cfduid=dcce85c35c3e5eb33524b0a1a79b6bf2b1548159374\r\n
accept-encoding: gzip, deflate\r\n
推荐人:https://carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207\r\n
主持人:www.carqueryapi.com\r\n
连接:keep-alive\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
连接:Keep-Alive\r\n
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)\r\n
Accept-Encoding: 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]
观察:
HeaderUser-Agent 的值不同。 API 阻止了对 Apache-HttpClient 的请求,但允许 PostmanRuntime/7.6.0.
更新代码使 header 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);
}
}
几个月来一直安心工作
从开源获取数据时 API 观察到当通过 Postman 进行 GET 调用时 API returns 200 和 valid/expected 收到数据。
写了下面的代码(Java 使用 Rest-Assured)从 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
===============================================
有人可以指导我如何解决这个问题吗?
经过大量搜索后,按照以下步骤操作:
使用 Wireshark 捕获邮递员和 rest-assured API 呼叫。
Postman API 调用包含以下值:
GET /api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207 HTTP/1.1\r\n
cache-control: no-cache\r\n
Postman-Token: c70faee6-f00c-47b6-9c6a-bb1c4ffe5cf4\r\n
User-Agent: PostmanRuntime/7.6.0\r\n
接受:/\r\n
cookie:__cfduid=dcce85c35c3e5eb33524b0a1a79b6bf2b1548159374\r\n
accept-encoding: gzip, deflate\r\n
推荐人:https://carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207\r\n
主持人:www.carqueryapi.com\r\n
连接:keep-alive\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
连接:Keep-Alive\r\n
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)\r\n
Accept-Encoding: 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]
观察:
HeaderUser-Agent 的值不同。 API 阻止了对 Apache-HttpClient 的请求,但允许 PostmanRuntime/7.6.0.
更新代码使 header 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);
}
}