Tuesday 23 May 2017

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?



via Garmekain

No comments:

Post a Comment