0

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lal
  • 1
  • 1

1 Answers1

0

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");
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • Thanks for the reply, I have tried the same as well but I'm still getting 407 status code. given().proxy("hostname", 8080).auth().preemptive().basic("username", "password") .relaxedHTTPSValidation().when().get("https://api.zippopotam.us/us/90210").then().assertThat().statusCode(200); – Lal Feb 23 '23 at 07:15