Lal
Lal

Reputation: 1

Getting 407 Authorization error while executing RestAssured code after providing custom proxy details

I'm new to API testing using Rest Assured. As I'm working behind the proxy, I have used all the proxy details in the code. Below is the code I'm trying to execute. Getting 407 error.

RequestSpecification httpRequest = RestAssured.given().relaxedHTTPSValidation()
        .proxy("hostname", 8080).auth().preemptive().basic("username", "password");
Response res = httpRequest.get("https://api.zippopotam.us/us/90210");
ResponseBody body = res.body();
res.statusCode();

String rbdy = body.asString();
System.out.println("Data from the GET API- " + rbdy);
System.out.println("Status code  : " +  res.statusCode());

Output: enter image description here

In postman with the same custom proxy setting, it is working fine. enter image description here

I have tried the code in other way as well, but getting the same out put.

RestAssured.proxy("host", 8080);
RestAssured.basic("username", "password");
// Authentication API is outside network and requires proxy

given().when().get("https://api.zippopotam.us/us/90210").then().statusCode(200);

Output: enter image description here

Can someone please help me on it?

I have also tried to set up the proxies in Eclipse itself, but it didn't work. I have changed propertied of "java.net.useSystemProxies=true" in the net.properties file inside jre (C:\Program Files\Java\jre1.8.0_351\lib), this didn't work either.

I have also used setProperty methods as well as below but no luck .

System.setProperty("https.proxyHost", "host");
System.setProperty("https.proxyPort", 8080);

same I did for username and password

Upvotes: 0

Views: 530

Answers (1)

CodeCamper
CodeCamper

Reputation: 6980

It appears that you are setting the proxy correctly but you are not passing the authentication details to the API endpoint.

To pass the authentication details to the API endpoint, you can use the auth() method of the RequestSpecification object, like you did in your first code snippet:

RequestSpecification httpRequest = RestAssured.given().relaxedHTTPSValidation()
        .proxy("hostname", 8080).auth().preemptive().basic("username", "password");
Response res = httpRequest.get("https://api.zippopotam.us/us/90210");

Upvotes: 0

Related Questions