1

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) => { 
  //set url to '/post-recieve' and set body / headers
})  

app.post('/post-recieve', (req, res, next) => {
  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.

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • @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. – ThomasReggi Feb 12 '16 at 02:00
  • 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. – Charlie Schliesser Feb 12 '16 at 02:05
  • 1
    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. – Charlie Schliesser Feb 12 '16 at 02:07

3 Answers3

1

This feels so dumb, but it might be the only way???

function postProxyMiddleware (url, data) {
  return (req, res, next) => {
    let str = []
    str.push(`<form id="theForm" action="${url}" method="POST">`)
    each(data, (value, key) => {
      str.push(`<input type="text" name="${key}" value="${value}">`)
    })
    str.push(`</form>`)
    str.push(`<script>`)
    str.push(`document.getElementById("theForm").submit()`)
    str.push(`</script>`)
    return res.send(str.join(''))
  }
}

app.get('/mock', postProxyMiddleware('/page', exampleHeaders))
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
0

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.

Dr. McKay
  • 2,727
  • 2
  • 15
  • 19
0

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.

chandoo
  • 1,276
  • 2
  • 21
  • 32