Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headers are not available in beforeRedirect #5365

Closed
claasahl opened this issue Dec 12, 2022 · 10 comments · Fixed by #5778
Closed

headers are not available in beforeRedirect #5365

claasahl opened this issue Dec 12, 2022 · 10 comments · Fixed by #5778

Comments

@claasahl
Copy link

Describe the bug

According to the documentation the configuration option beforeRedirect should be called with response headers but it does not. Only the options parameter is passed to beforeRedirect (i.e. no headers parameter).

  // `beforeRedirect` defines a function that will be called before redirect.
  // Use this to adjust the request options upon redirecting,
  // to inspect the latest response headers,
  // or to cancel the request by throwing an error
  // If maxRedirects is set to 0, `beforeRedirect` is not used.
  beforeRedirect: (options, { headers }) => {
    if (options.hostname === "example.com") {
      options.auth = "user:password";
    }
  },

(copied from https://www.npmjs.com/package/axios/v/1.2.1#request-config)

This seems to be a duplicate of #5176.

To Reproduce

//var axios = require("axios@0.27.2") // works
//var axios = require("axios@1.0.0") // does not work
//var axios = require("axios@1.1.1") // does not work
//var axios = require("axios@1.1.2") // does not work
//var axios = require("axios@1.1.3") // does not work
//var axios = require("axios@1.2.0-alpha.1") // does not work
//var axios = require("axios@1.2.0") // does not work
var axios = require("axios") // does not work

// Works with an older version
// var axios = require("axios@0.27.2")
var express = require("express")
var app = express()

app.get('/redirect', (req, res) => {
  res.cookie("Hello", "World");
  res.redirect("/");
})

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(3000, () => {
    axios.get("http://localhost:3000/redirect", {
        // Headers undefined
        beforeRedirect: (options, { headers }) => {
            console.log({options, headers});
        }
    });
});

Code snippet

No response

Expected behavior

beforeRedirect should be called with both the options and headers parameters.

Axios Version

1.2.1

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

No response

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

@exander77
Copy link

I can confirm.

@dickorydock
Copy link

Appreciate the work that has gone into making this package. I tried the fix mentioned in #5176 (installing a different release,. axios@1.2.0-alpha.1) but that didn't seem to work and accessing headers using beforeRedirect still seems to be broken.

@lastelok
Copy link

confirm issue, 1.3.5 still not works

@k1de
Copy link

k1de commented Apr 18, 2023

I confirm too

@reciprocal-space
Copy link

+1 can confirm the problem persists on v1.3.6

@lastelok
Copy link

lastelok commented May 3, 2023

pls fix, want use latest version but cant

@alexey-sh
Copy link

1.4.0 doesn't work as well

@k1de
Copy link

k1de commented Jun 2, 2023

I guess this is the solution 8b5cab4

@marcusdelang
Copy link

Still doesn't work in 1.6.1.
@jasonsaayman Please help merging any of the PRs 🙏

@pro-vi
Copy link

pro-vi commented Jan 23, 2024

Encountered in 1.6.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants