How can I get responses from redirections?

Hi all,

I am making a POST request that makes some redirections. First redirection returns 302, second returns 302 and last one returns 200 as status code.

response = http.post(
        "MY_URL",
        {
          email: "MY_MAIL",
          password: "MY_PASS",
          __RequestVerificationToken: requestVerificationToken,
        },
        {
          headers: {
            "content-type": "application/x-www-form-urlencoded",
            origin: "MY_ORIGIN",
            referer:
              "MY_REFERER",
            "upgrade-insecure-requests": "1",
            "user-agent":
              "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
            "sec-ch-ua":
              '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
          },
        });

The problem is that, response only gives me the latest status code (200) and the latest response body and headers. I need a token that it is provided in the second redirect, but I have no access to that information. Is there any way to access there? I’m happy with the automatic redirection but I’d like to get that info from the “hidden” requests.

Thanks a lot in advance for your help.

Kind regards,

Igor

1 Like

Hi @igorrecioh,
welcome to the community forum :tada:

The HTTP JS API supports the redirects param where you can set the maximum number of redirects to follow. You could limit the number of redirects for the first request, get the token then execute the rest of the redirects with a second request.

Here some simplified code for summary:

var response = http.post(<URL>, {redirects: 1})
var token = response.<TOKEN-PATH> 
response = http.post(<CONTINUE-REDIRECT-URL>)
1 Like

Hi!

It works as you say :smile: ,but I think that it would be great to have the ability to search on a more convenient way, through all the responses that a request has generated.

Is this feature on the roadmap? Which would be the best place to share this idea with the community?

Thanks in advance,

1 Like

Hi @andonima,

It mostly depends on what you mean with ability to search. k6’s supports an HTTP debugging mode that prints all the entire http flow triggered by requests. Do you need more?

If you have a defined feature request you can open a new issue directly in the k6’s repo, otherwise, you can reply here if it helps with the original @igorrecioh’s request or open a new one explaining your idea.

Imagine we have a request that behaves like this (@igorrecioh 's situation):

Original Request: POST /xxx
Response: HTTP 302 headersA

Redirection Request 1: GET /yyy
Response: HTTP 302 headersB

Redirection Request 2: GET /zzz
Response: HTTP 200 headersC

And we want to get some data of headersB.

The idea you proposed was to use “{redirects: 1}” to get the response.headers of that request and to add the following redirection as a new request.

This works perfect, but I see 2 downsides to it:

  • It requires to know exactly in which redirection you receive the data you need to correlate.
  • It is not very convenient if you have a lot of redirections to handle.

In my opinion, it would be perfect to receive, for example the list of all the responses that your request has unchained to be able to correlate data from any of those answers.

Have I explained myself better? :sweat_smile: :sweat_smile:

Thanks!

1 Like

Hi @andonima,
thanks for sharing. I created an issue with your suggestion.

Having it enabled for each redirect by default in k6, it could have some performance impacts so it should be better to add it as a JavaScript’s helper function.

1 Like

Hi @codebien
I am trying with the httpx and still getting the same results. Whether http or httpx, response only gives me the latest status code (200) and the latest response body and headers.

Hi @VijayY,
we opened an issue for the httpx but we haven’t yet worked on it. The httpx project is open source, you can consider opening a pull request.