Get the url of a redirect with Rest Assured?
Asked Answered
K

1

13

I am making a GET request which then makes a 307 redirect to another URL and from there it makes another 302 redirect and so on till it gets to the requested page. I am having problems extracting the URL from the first redirect, the 307, I want to see the location where is redirected to. I am using Rest Assured as framework. Thanks!

Kingly answered 27/3, 2017 at 14:24 Comment(0)
B
20

I had the same problem, but I didn't have the 307, just 302. I imagine the solution would be the same. What I did was:

  1. stop following the redirects on the first call with redirects().follow(false)

  2. capture the URL from the first redirect

  3. make another call to follow the redirect(s)

    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);
    
Bromidic answered 2/6, 2017 at 10:7 Comment(1)
Sorry, but in my case, it is not working. I get 200 but I dont get back the json response. I get an empty response string. In fiddler/postman, I correctly get the json response. but not in rest assured.Pneumatics

© 2022 - 2024 — McMap. All rights reserved.