Title: Send a POST request on the server with Express.js
Tags: javascript;node.js;express
Question: I'm running into a small issue with something I thought was possible.
I want to have two express routes, one GET route /post-data and one POST route /post-recieve.
The code would look something like this:
app.get('/post-data', (req, res, next) =&gt; {
//set url to '/post-recieve' and set body / headers
app.post('/post-recieve', (req, res, next) =&gt; {
return res.json(res.body)
Now, when you visit /post-data you should be instantly redirected to /post-recieve except if you look in the developer console you should see that the method for the document is POST and not a normal GET request.
Is this possible?
I know you can use a library like request to make a HTTP post request to an endpoint, but I'm talking about actually sending the user to the page via a POST request.
Comment: @adeneo I have a request coming in from a third party and is hitting my server. I'd like to be able to mock that POST request on my server.

Comment: See http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get and https://en.wikipedia.org/wiki/Post/Redirect/Get. It is my understanding that the client is only capable of issuing the request method, e.g. `GET /index.php HTTP/1.1; Host: www.example.org`, and a redirect header from the server is followed by the client using the same method, e.g. `HTTP/1.1 301 Moved Permanently; Location: http://www.example.org/index.asp`. From your comment, it sounds like you want to act as proxy, in which case *you* would perform the POST and then send the response to the client.

Comment: Interesting question, btw! There are a lot of other questions on s.o about changing the request method, so you can do some more searching here to get more info.

Here is the accepted answer: This feels so dumb, but it might be the only way???
function postProxyMiddleware (url, data) {
return (req, res, next) =&gt; {
let str = []
str.push(`<form id="theForm" action="${url}" method="POST"&gt;`)
each(data, (value, key) =&gt; {
str.push(`<input type="text" name="${key}" value="${value}"&gt;`)
})
str.push(`</form&gt;`)
str.push(`<script&gt;`)
str.push(`document.getElementById("theForm").submit()`)
str.push(`</script&gt;`)
return res.send(str.join(''))
app.get('/mock', postProxyMiddleware('/page', exampleHeaders))

Here is another answer: The only way to change the client's request method from GET to POST programmatically is to create a form containing hidden elements with method="post" and action="/post-receive", then using client-side JavaScript to automatically submit the form.
Any HTTP redirects in response to a GET request will also be GET.

Here is another answer: You can use request-promise to post the data to a url. So, initiate with this function and you can get the data in the api url
const request = require('request');
const rp = require('request-promise');
let req = {
"param1" : "data1",
"param1" : "data2"
}
rp({
method: 'POST',
uri: 'http://localhost:3000/post-data/',
body: req,
json: true // Automatically stringifies the body to JSON
}).then(function (parsedBody) {
console.dir(parsedBody);
return parsedBody;
// POST succeeded...
})
.catch(function (err) {
console.log(err+'failed to post data.');
return err+'failed to post data.';
// POST failed...
});
Apologies If I get your question wrong.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:02/12/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><node.js><express>', 'Title': 'Send a POST request on the server with Express.js', 'ViewCount': 1126, 'id': 35353480, 'user_AboutMe': '', 'user_AccountId': 136879.0, 'user_DisplayName': 'ThomasReggi', 'user_Reputation': 50241.0, 'user_Views': 3866.0, 'user_id': 340688.0}}