3

Is it possible to retrieve the body contents using express?

I started by trying body-parser but that doesn't seem to work with GET. Are there any modules which would work?

var express = require('express'),
  bodyParser = require('body-parser'),
  PORT = process.env.PORT || 4101,
  app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.route('/')
  .get(function(req, res) {
    respond(req, res, 'GET body contents:\n');
  })
  .post(function(req, res) {
    respond(req, res, 'POST body contents:\n');
  });

app.listen(PORT, function(err) {
  if (err) {
    console.log('err on startup ' + err);
    return;
  }
  console.log('Server listening on port ' + PORT);
});

/*
 * Send a response back to client
 */
function respond(req, res, msg){
  res.setHeader('Content-Type', 'text/plain');
  res.write(msg);
  res.end(JSON.stringify(req.body, null, 2));
}

This is response from GET:

GET body contents:
{}

And from POST:

POST body contents:
{
    "gggg": ""
}
bobbyrne01
  • 5,464
  • 13
  • 60
  • 127
  • 1
    Primo, GET request's does not have body – Héctor Apr 05 '16 at 11:34
  • 1
    take a look at this: http://stackoverflow.com/questions/978061/http-get-with-request-body – lauriys Apr 05 '16 at 11:34
  • GET request's does not have body. Use POST instead. – Sapikelio Apr 05 '16 at 11:39
  • @bigdestroyer GET requests do allow a body – bobbyrne01 Apr 05 '16 at 11:41
  • 2
    @bobbyrne01 While GETs can have a body, as per the HTTP spec, servers should ignore it, as it has no meaning. See [this answer](http://stackoverflow.com/a/983458/789529). What is the server going to do with all these UUIDs? It's probable that structuring the application differently is what's needed, an ultra-long URL is unlikely to be the right tool for the job... – Mark Hughes Apr 05 '16 at 13:15

1 Answers1

5

GET requests don't have a body, they have query strings. In order to access a query string in expressJS you should use the req.query object.

res.end(JSON.stringify(req.query, null, 2));
Björn Böing
  • 1,469
  • 10
  • 20
Vsevolod Goloviznin
  • 11,256
  • 1
  • 39
  • 47
  • The problem is, I'd like user to submit a list of `uuids` in the `GET` request body. I will lookup these `uuids` against a db. There could be hundreds of `uuids` passed, should these all be put into `query` string? – bobbyrne01 Apr 05 '16 at 11:44
  • @bobbyrne01 Why can't you use a POST request then? There is a limitation on the length of a query string, but it's fairly large – Vsevolod Goloviznin Apr 05 '16 at 11:47
  • I can use `POST`, but was trying to remain as RESTful as possible. I understand the limitation on length is large, but theres a possibility I could hit it. – bobbyrne01 Apr 05 '16 at 11:53
  • 1
    It all depends on architecture and requirements. For example you can limit the number of items that can be processed in a batch, or you can make the whole process async, where you post the batch and then check the status of the batch – Vsevolod Goloviznin Apr 05 '16 at 11:56
  • @bobbyrne01 If you want to remain RESTful, use GET headers. They achieve the same effect as POST (etc) body. Have a look at https://www.owasp.org/index.php/REST_Security_Cheat_Sheet#Sensitive_information_in_HTTP_requests – aggregate1166877 Feb 25 '18 at 05:06