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

Following redirect after POST request throws an exception #1616

Open
the-other-michael-meier opened this issue Aug 2, 2022 · 4 comments · May be fixed by #1790
Open

Following redirect after POST request throws an exception #1616

the-other-michael-meier opened this issue Aug 2, 2022 · 4 comments · May be fixed by #1790
Labels

Comments

@the-other-michael-meier
Copy link

When a POST request with redirect: follow is answered with a 307 status code, an exception is thrown due to accessing an undefined options object.

Reproduction

Given a minimal server pseudo-setup like this:

import express from "express";
const app = express();
const port = 3000;

app.use(express.static("public"));

app.post("/", (req, res) => {
  res.redirect(307, "/foobar");
});

app.post("/foobar", (req, res) => {
  res.send("ok");
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Using fetch like this:

import fetch from "node-fetch";
import { Request } from "node-fetch";

const requestinfo = {
  method: "POST",
  redirect: "follow",
};

requestinfo.body = JSON.stringify({ foo: "bar" });

const request = new Request("http://localhost:3000/", requestinfo);
const response = await fetch(request);

results in following error:

file:///Users/[...]/node_modules/node-fetch/src/index.js:216
                                                if (response_.statusCode !== 303 && request.body && options_.body instanceof Stream.Readable) {
                                                                                                             ^

TypeError: Cannot read properties of undefined (reading 'body')

Expected behavior

Follow the redirect and send the POST request to the new location.
This is how fetch in the browser seems to behave (tested with Chrome and Firefox).

As the second parameter to fetch is optional, the behavior should be the same as calling fetch(request, {}), which works as expected.

Your Environment

software version
node-fetch 3.2.9
node 16.16.0
npm 8.11.0
Operating System Tested on MacOS and Ubuntu 22.04
@markcellus
Copy link

Having this same issue. Downgrading to version 2.6.7 fixes it. Something must have been done in later versions that causes this error again.

@sabinadams
Copy link

I'm seeing this issue as well.

@alivtar
Copy link

alivtar commented May 15, 2023

I am also facing the same issue. Any updates on this problem?

@emilniklas emilniklas linked a pull request Nov 16, 2023 that will close this issue
@NabiKAZ
Copy link

NabiKAZ commented Dec 13, 2023

I also have this problem and it seems that the problem is solved with the newly opened PR.
But after 16 months since the issue was opened, it still hasn't been merged.

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

Successfully merging a pull request may close this issue.

5 participants