Issue
I have been working with rest assured since few months.
While getting data from an open source API observed that when GET call is made via Postman API returns 200 and valid/expected data is received.
Wrote below code(Java using Rest-Assured) to get the same data from 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);
}
}
Code output is:
[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
===============================================
Can someone guide me how to overcome this issue?
Solution
After lot of search followed below steps:
Used Wireshark to capture postman and rest-assured API calls.
Postman API call contains following values:
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
Accept: /\r\n
cookie: __cfduid=dcce85c35c3e5eb33524b0a1a79b6bf2b1548159374\r\n
accept-encoding: gzip, deflate\r\n
referer: https://carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207\r\n
Host: www.carqueryapi.com\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://www.carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207]
[HTTP request 1/1] [Response in frame: 350]
Rest-Assured API call contains following values:
GET /api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207 HTTP/1.1\r\n
Accept: /\r\n
Host: www.carqueryapi.com\r\n
Connection: 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
[Full request URI: http://www.carqueryapi.com/api/0.3/?cmd=getTrims&make=Abarth&year=1955&model=207]
[HTTP request 1/1]
[Response in frame: 769]
Observation:
Value of Header User-Agent was different. And the API was blocking requests for Apache-HttpClient but allowing for PostmanRuntime/7.6.0.
Updated code to have header User-Agent with value PostmanRuntime/7.6.0 and it worked.
Below is working code:
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);
}
}
Answered By - Aks.Soms
Answer Checked By - David Marino (JavaFixing Volunteer)