0

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));

Paul
  • 1
  • 1
  • this link might help you http://biercoff.com/why-rest-assured-doesnt-redirect-post-requests/ – lucas-nguyen-17 Nov 06 '22 at 13:51
  • I already tried it, but unfortunately it did not work. Thank you for your comment anyway. – Paul Nov 06 '22 at 19:28
  • I am confused about the ways you have used. What you shown is rather about limiting redirects rather than forcing them to happen. – Alexey R. Nov 07 '22 at 16:53
  • I tried to do like you shared below, but it gave me null value for cookies and etc. Could you PLEASE change above code and put it here how it should be? – Paul Nov 09 '22 at 02:16

1 Answers1

0

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);
Alex Karamfilov
  • 634
  • 1
  • 6
  • 12