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.

Here’s how you can modify your code to use http.request()…

import { check } from 'k6/http';
import { sleep } from 'k6';

// Perform the first request
const response1 = http.request(
  "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"',
    },
  }
);

// Handle the redirection manually
let redirectUrl = response1.headers["Location"];
let numRedirects = 0;
while (redirectUrl && numRedirects < 10) {
  // Wait for a short interval between redirects
  sleep(1);
  
  // Perform the next request based on the redirect URL
  const response = http.request("GET", redirectUrl);
  
  // Check if the response is a redirect (3xx status code)
  if (response.status >= 300 && response.status < 400) {
    // Extract the new redirect URL
    redirectUrl = response.headers["Location"];
  } else {
    // If it's not a redirect, stop the loop
    break;
  }
  
  // Increment the number of redirects and ensure we don't get stuck in an infinite loop
  numRedirects++;
}

// Use the final response after all the redirects have been followed
check(response, {
  "Response status code is 200": (r) => r.status === 200,
});

// Now you can access the final response body and headers
console.log(response.body);
console.log(response.headers);

You can continue this process until you reach the final response (status code 200) or a predefined number of maximum redirects to avoid getting stuck in an infinite loop.
You can verify your URL in this tool Redirect Checker to get its redirection chain and its status code.