简体   繁体   中英

Change POST data before redirecting with expressjs?

This is basically the code I have:

// in file1.php

<?php print_r($_POST); ?>
<form method="POST" action="http://localhost:1234">
    <button type="submit" value="3" name="q">Register</button>
</form>

// in server.js

app.post('/', (req, res) => {
    request.post(
        req.headers.referer,
        { form: { test: "mytest" } }, 
        function (error, response, body) {
        if (!error && response.statusCode == 200) {
            // Print out the response body
            res.set("POST", "test=1234");
            res.redirect(302, req.headers.referer);
        }
    });
});

app.listen(1234);

This is what this code needs to do:

When I am in file1.php and I click the Register button, it should send a post request back to file1.php with { test: "mytest" } as post headers from http://localhost:1234 ; file1.php does something with that post request and sends back the response to http://localhost:1234 (inside the callback in request.post() ), then, it should change the POST headers to { test: 1234 } , redirect to file1.php and show Array([test]=>1234) [Register button] in the screen.

The problem is that I cannot get it work to appear that text in the screen, it seems that headers are not changing or I am not changing them well. What can I do?

I think you're confusing headers with the request body and header names with the HTTP method.

With this:

res.set("POST", "test=1234");

you can only change the response header and not tell the client to use this header after following the redirect.

Also, setting a header named POST will not pass the data put in the value of that header in a body of a POST request.

It seems that you are trying to make a redirect result in a POST request with certain fields passed in the body but what you're doing here is setting a header named POST with something that you presumably want to be set in the body. It will not work this way.

A normal redirect (301 Moved Permanently or 302 Found) will always result in a GET request, even if the redirect is a response to POST because in practice 301 and 302 work like 303 See Other resulting in a GET request. There is a 307 Temporary Redirect that explicitly forbids changing the HTTP method but browsers should warn users before following such redirects and it may not always work. See this answer for more details:

There is no way to alter the client's POST request when redirecting in this way, unless of course you want to proxy the request by having the server do the redirect on the client's behalf.

But there's some solutions for you, first use 307 instead of 302 to redirect your post request with body

res.redirect(307, "http://badasse.com/api/t2");

Then you can either pass additional datas through GET parameters.

res.redirect(307, "http://badasse.com?morePost="+encodeURIComponent(JSON.stringify({data1: "yolo"}));

Or headers:

 res.setHeader("MyHeader", "MyStringifiedDatas")

Or simply try to use session

req.session['success'] = 'User added successfully';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM