0

I have a POST request, which I tried first in Postman, I wanted to capture the status code before it's redirected. In Postman, I got 307 (I set the settings so it doesn't follow redirects).

Postman

But when I tried using restassured, it still got redirected, so I got 200 status code instead of 307.

Tried the same way with GET request with 302 status code, and that one works.

public void postDataBeforeLogin() {
    
    //post data before login
    Response response = RestAssured.given().redirects().follow(false).post("/data");
    
    assertEquals(response.getStatusCode(), 307); 

}

I read an article/post about restassured not redirecting POST requests, but it was from 3 years ago, so I'm not sure if that is still the case.

Can anyone help/clarify?

Help will be greatly appreciated, thank you!

Far
  • 1

1 Answers1

1

RestAssuredConfig will meet your requirements, like this:

given().config(RestAssured.config().redirect(redirectConfig().followRedirects(false))).

in your case:

import static io.restassured.config.RedirectConfig.redirectConfig;

Response response = RestAssured.given()
                    .config(RestAssured.config().redirect(redirectConfig().followRedirects(false)))
                    .post("/data");

See more: REST-assured wiki

It's recommended to statically import methods, see this

Peter Quan
  • 788
  • 4
  • 9
  • 'redirectConfig() is undefined' error message on my end, is there something i should define first, or extra dependencies? a quick google search tells me it's already in restassured. thank you! – Far Sep 08 '20 at 06:08
  • @Far `redirectConfig()` is a staic method, so you need static import its class `import static io.restassured.config.RedirectConfig.redirectConfig;` – Peter Quan Sep 08 '20 at 07:01
  • ok imported and used the code, but it's still coming up as 200 and not 307 like it's showing up in postman :( – Far Sep 09 '20 at 08:15
  • Sorry to say that I have no idea now, and I have no way to debug the code further. – Peter Quan Sep 09 '20 at 09:16
  • that's alright, i learned about config, so thank you for the help! – Far Sep 10 '20 at 10:02