Search code examples
curlrest-assuredrest-assured-jsonpath

How to have location header returned from RestAssured response


I'm using RestAssured and I need to get the value set under the location header that is returned by my api so that I can use it as a bearer token on my next request.

enter image description here

I tried some things like cookieHeader but I dont think that's it.

    Response response = getRequestSpecification()
            .queryParam("myparam", "id_token")
            .queryParam("redirect_uri", "https%3A%2F%2Fmyurl.localhost%3A3000%2Fview")
            .get(OKTA_OAUTH2_AUTHORIZE);


public static RequestSpecification getRequestSpecification() {

        /** Enables printing request as curl under the terminal as per https://github.com/dzieciou/curl-logger */
        Options options = Options.builder()
                .printMultiliner()
                .updateCurl(curl -> curl
                        .removeHeader("Host")
                        .removeHeader("User-Agent")
                        .setCookieHeader("location")
                        .removeHeader("Connection"))

                .build();

        RestAssuredConfig config = CurlRestAssuredConfigFactory.createConfig(options).objectMapperConfig(new ObjectMapperConfig(ObjectMapperType.GSON));


        RequestSpecification rq = given()
                .config(config)
                .contentType(ContentType.JSON)
                .cookie("location")
                .when()
                .log()
                .everything();

        return rq;
    }

I would like my curl to have the --location added to it like this:

curl --location --request GET 'https:myapi.com' \

After that is done, I'll also need to append -i into it.

Ps: I'm using this library to print the curl.

Thank you very myuch.

UPDATE: I think it may have something to do with disabling redirects, as when I have this enabled on Postman I get a 400 and no header returned, but when they are turned off I get a 302 which also gives me the location. I've then tried to add this to my request.

.redirects().follow(false)

so that I have

Response response = getRequestSpecification()
        .queryParam("myparam", "id_token")
        .redirects().follow(false)
        .get(OKTA_OAUTH2_AUTHORIZE);

but I'm still getting the 400 error there.


Solution

  • It's working now. There were two issues:

    • An attempt to redirect to the redirect_uri was being made, so I had to disable that.

    • The url was being encoded in a different way than was Postman uses, so I disable that also and added it already encoded manually as the param I needed.

      Response response = getRequestSpecification()
                    .queryParam("myparam", "id_token")
                    .queryParam("redirect_uri", "https%3A%2F%2Fmyurl.localhost%3A3000%2Fview")
                    .redirects().follow(false)
                    .urlEncodingEnabled(false)
                    .get(OKTA_OAUTH2_AUTHORIZE);