How can I turn ON "Automatically follow redirect" in Rest Assured/Java

1.3k views Asked by At

I'm stuck on this issue, please help...

So, Post request working fine on Postman, but in Rest Assured I am getting 307 Temporary Redirect status code. I find that this issue related to redirects. Because I checked settings on Postman and when I am turning "Automatically follow redirect" toggle OFF, it gives same issue in Postman as well, but it works when it is ON.

How can I add the same to my code (turn ON "Automatically follow redirect" in Rest Assured/Java?)

Here is my code:


static {
    baseURI = ConfigReader.getProperty("url");
}

RequestSpecification requestSpecification;
Response response;
String accessToken;


@Given("I am authorized at endpoint {string}")
public void getAccessToken(String endpoint) {

    response = given().log().all()
            .header("Content-Type", "application/json").body("{\n" +
                    " \"username\": \"" + ConfigReader.getProperty("username") + "\",\n" +
                    "\"password\": \"" + ConfigReader.getProperty("password") + "\" \n" +
                    "}").post(endpoint);
    String jsonString = response.asString();
    accessToken = JsonPath.from(jsonString).get("AccessToken");
}


@When("I add the header {string} {string}")
public void setAuthorizationHeaders(String key, String value) {

    requestSpecification = given()
            .header(key, value).
            header("AccessToken", accessToken);
}


 @Then("I send a POST request to {string} endpoint with the body {body}")
    public void iSendAPOSTRequestToEndpointWithTheBody(String string, String body) throws InterruptedException {

        requestSpecification.body(new File("body"))
                .post(string).then().log().all().
                 statusCode(200);
    }

I already used these ways and they did not work (or I used them wrong):

Please advise based on my above code!

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

RequestSpecification spec = new RequestSpecBuilder().setConfig(RestAssured.config().redirect(redirectConfig().followRedirects(false))).build();

RestAssured.config = config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));

1

There are 1 answers

0
Alex Karamfilov On

Rest assured will automatically redirect if it is GET/HEAD request and status code is 302. In your case this is a POST request so it wont automatically redirect. You might need to do it manually like:

Response resp1 =
        given().
            contentType(ContentType.URLENC).
            body("AUTH_TOKEN=&j_username=" + encodedUsername + "&j_password=" + password + "&userName=&AUTH_TOKEN=").
            redirects().follow(false).
        expect().
            statusCode(302).
        when().
            post("/authenticate/j_spring_security_check");

String headerLocationValue = resp1.getHeader("Location");

Response resp2 =
        given().
            cookie(resp1.getDetailedCookie("JSESSIONID")).
        expect().
            statusCode(200).
        when().
            get(headerLocationValue);