0

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.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

1 Answers1

0

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);
    
Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
  • Hi, I'm using this code, private final Options options = Options.builder().useLogLevel(Level.INFO).build(); private final RestAssuredConfig config = CurlRestAssuredConfigFactory.createConfig(options); RequestSpecification spec = getRequestSpecification(RestAssured.given()); return spec.config(config).when().get(this.url).then().extract().response(); Some logs are generated after last piece of code as shown in console , would it be possible to fetch those logs ? Thanks a lot – Diksha Baluja Jul 22 '21 at 03:53
  • Hi, sorry, not sure I understand the question. What do you mean by fetching the logs? – Francislainy Campos Jul 22 '21 at 07:34
  • I meant that in console, some logs are generated , like INFO curl -- some http request , afterwards headers, form data etc.. so I have to pass that information in the report , hence need to store them like a string value – Diksha Baluja Jul 22 '21 at 09:47
  • The printing for the curl comes from a library and is controlled by a logback.xml file under the resources folder. There you can define where it should be printed, whether to the terminal or to a file. – Francislainy Campos Jul 23 '21 at 11:28
  • This may help you: https://stackoverflow.com/a/63843436/6654475 – Francislainy Campos Jul 23 '21 at 17:51
  • Thanks , I got it, it was in readme only , didn't notice earlier , your logback suggestion really helped https://github.com/dzieciou/curl-logger#custom-curl-handling – Diksha Baluja Jul 24 '21 at 15:22