0

I've been working on creating an API for a blockchain I've made.

Everything was going fine, until I updated Node and Windows...

====== ISSUE ====== After performing a POST request, I cannot redirect to a GET page anymore. (Using Postman only no front end yet).

For some reason, it was working fine and now I can't do it in a simple test file even.

I've attached the simple express test file below (which duplicates my problem).

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("Hello World from GET!");
});

// BELOW IS THE WAY I'VE WORKED AROUND IT FOR NOW
// app.post("/", (req, res) => {
//   res.send("Hello World from POST!");
// });

app.post("/me", function (req, res) {
  res.redirect("/");  // ISSUE HAPPENS HERE
});

var server = app.listen(8081, () => {
  var port = server.address().port;
  console.log(`Example app listening at http://localhost:${port}`);
});

Using PostMan, now whenever I try to redirect after app.post("/me"), it will return a 404 error shown below...

<body>
    <pre>Cannot POST /</pre>
</body>

I found out if I add a POST method to the "/" route (commented out above), it will work correctly.

Why didn't I have to do this before??? Did I break my computer? lol

I have tried uninstalling Node v19 and reinstalling Node v16. (no change)

I uninstalled the Windows updates that I did when restarting for Node v19 installation.

  • Could it be something to do with `body-parser`? The example doesn't seem to need it, so I'd be curious if you still had the problem after commenting out the two related `app.use` lines for it. – machineghost Dec 27 '22 at 06:06
  • That didn't change anything. I even uninstalled it and deleted the lines using it. My blockchain API has had body-parser the whole time. EDIT: I deleted the stuff you mentioned for simplification. – Taylor Lepper Dec 27 '22 at 06:11

2 Answers2

2

Your browser makes a POST / request after redirection, which your app does not accept, it expects a GET / request instead.

Whether a redirection after a POST request leads to a GET request or another POST request depends on the HTTP status (between 301 and 308) as well as on the browser. Perhaps the Windows update changed the default behavior of your browser?

To avoid any ambiguity, you should use

app.post("/me", function (req, res) {
  res.redirect(303, "/");
});

See also Google Apps Script return 405 for POST request.

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • Thank you! Using the status code didn't do it for me, but after reading thru the post you provided, I found a link to changing Postman settings (near Body, Headers etc). Changing "Follow original HTTP Method" to OFF solved it for me! Now it uses the default status 302 correctly and redirects with a "GET" request properly. – Taylor Lepper Dec 27 '22 at 10:23
0

Changing Settings

I deactivated "Follow original HTTP Method" in PostMan request settings (near body and headers).

This solved my problem. PostMan was executing another POST request on my redirect, instead of a GET.

Following the link Heiko provided above, and another link on that page about changing PostMan settings, led me to this page...

https://makolyte.com/postman-follow-redirects-with-the-original-http-method-and-content-type/

I was able to disable the "Follow original HTTP Method" which will "Redirect with the original HTTP method instead of the default behavior of redirecting with GET" while it is activated.

It appears this is a per request change in PostMan however.

Not sure exactly what caused this to happen, but it works!

I also found out about the PostMan console, located at the bottom of the screen. This was helpful in debugging the issue.