0

I need to redirect a POST request, and to demonstrate the issue I have setup 2 nodejs servers. One serving some HTML and doing the mod-rewrite and the other receiving the POST after the rewrite. The first one which serves HTML and doing the rewrite looks like this:

var connect = require('connect');
var cors = require('cors');
const modRewrite = require('connect-modrewrite')

var http = require('http');
const { nextTick } = require('process');

var app = connect();

app.use(cors())


app.use('/test', function(req, res, next){
    res.end(`
        <!doctype html><html><body>

        <button type="button"class="yes">click</button>

        <script>
            async function postData (url = '', data = {}) {
                const response = await fetch(url, {
                    method: 'POST',
                    mode: 'cors',
                    cache: 'no-cache', 
                    credentials: 'same-origin',
                    headers: {
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Methods': 'DELETE, POST, GET, OPTIONS'
                    },
                    redirect: 'follow', // manual, *follow, error
                    referrerPolicy: 'no-referrer', // no-referrer, *client
                    body: JSON.stringify(data)
                })
                return await response.json();
              }

              document.querySelector('.yes').addEventListener('click', () => {
                postData('http://localhost:3000/a/b', { answer: 42 }).then(data => {
                    console.log(data); 
                });
            });
        </script></body></html>`);
    });

    app.use(function(req, res, next){
        console.log('RECEIVED', req.method);
        next();
    });

    app.use(modRewrite([
        '^/(.*)$ http://localhost:4001/$1 [R, L]'
    ])
);

http.createServer(app).listen(3000);

So the line '^/(.*)$ http://localhost:4001/$1 [R, L]' rewrites the POST to my other backend server:

var connect = require('connect')
var cors = require('cors')
var http = require('http')

var app = connect()
app.use(cors());
app.use(function (req, res) {
   console.log('RECEIVED', req.url, req.method);
  res.end(JSON.stringify({status: 'ok'}));
});

http.createServer(app).listen(4001

So the flow is as follows:

1) In the browser I do a fetch/POST
2) It is received as POST and connect-modrewrite is applied
3) My second backend prints: RECEIVED /a/b GET

So my question is, why is my POST transformed into a GET and how can I fix this?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

1

This is expected behavior. When you do a redirect (301/302), POST data is discarded on redirect as a client will perform a GET request to the URL specified by the 301.

In order to fix this, you should use redirect 307. See this for reference: https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect#99966

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54