Response headers in browser are different from k6

Hi,

I’m trying to extract location from the response headers and couldn’t find in the k6 response but the same is found in browser response headers. Am I missing something?

Thanks

Hi there, welcome to the forum :slight_smile:

Can you share a sample script that reproduces your issue, so we can take a look?

Apologies for delayed response.

Below are some response headers that I get from network tab of Okta authentication, and yes I know for Okta authentication there is a simpler way in k6 and have used it to solve my problem and that’s why I’ve completely forgot about this thread.

HTTP/1.1 302 Found
Date: Fri, 28 Apr 2023 22:06:45 GMT
Server: nginx
Content-Length: 0
x-okta-request-id: <<>>
x-xss-protection: 0
p3p: CP=“HONK”
x-rate-limit-limit: 600
x-rate-limit-remaining: 599
x-rate-limit-reset: 1682719665
cache-control: no-cache, no-store
pragma: no-cache
expires: 0
referrer-policy: no-referrer
location: https://<<>>/users/auth/oktaoauth/callback?code=<<>>&state=<<>>

But when I try to get the same headers from response.headers I don’t see location in it.

Hi, Did you find any solution for extracting location from the response headers ?

k6 follows redirects automatically by default. With automatic redirects, the response object returned by the call that initiates the redirect chain is actually that of the final response received, which would typically be something in the 2xx range and not the 301/302/303 that would contain the Location header.

In some situations, it may be necessary to extract some value from this Location header. If that’s the case, you would have to manually traverse the redirect chain by setting redirects: 0 in the initiating request’s params.

Here’s a runnable example:

import http from 'k6/http'

export default function () {
  // automatic redirects are enabled by default
  const res1 = http.get('https://httpbin.test.k6.io/redirect-to?url=https%3A%2F%2Fk6.io&status_code=302')

  // as a result, the response will be the response from the final URL, which should have a status code of 200
  console.log(`res1 status code: ${res1.status}`)

  // same request as above, but now with redirects disabled
  const res2 = http.get('https://httpbin.test.k6.io/redirect-to?url=https%3A%2F%2Fk6.io&status_code=302', {
    redirects: 0
  })

  // if we disable redirects, we'll get the response from the first URL, which should have a status code of 302
  console.log(`res2 status code: ${res2.status}`)

  // the Location header will therefore be available
  const locationHeader = res2.headers.Location
  console.log(`res2 Location header: ${locationHeader}`)
  
  // optionally extract something from the Location header, e.g. with findBetween (see https://k6.io/docs/javascript-api/jslib/utils/)

  // we can then easily continue the redirect chain by simply passing in the Location header value:
  const res3 = http.get(locationHeader)

  console.log(`res3 status code: ${res3.status}`)
}
1 Like