1

I'm new to Node/Restify and must be missing something stupid here, but I've been banging my head on this for awhile. Hopefully someone can help.

I'm trying to read Body parameters from a POST request with restify. Everything I find seems to suggest that all I need is server.user(bodyParser()), but its not working and I don't know why, and I'm not really sure how to troubleshoot further.

Can anyone help point me in the right direction on what I'm doing wrong here?

I created a simple server like follows:

  var restify = require('restify');

  var server = restify.createServer();
  server.use(restify.bodyParser());

  server.post("/test", function(req, res){
    console.log(req.params);
    console.log(req.body);
    res.send(200);
    res.end();
  });

  server.listen(8081);

I then ran the following POST request:

POST /test?GET_PARAM=def HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 33

POST_PARAM1=abc&POST_PARAM2=ghi

This results in the following in the console:

debugger listening on port 49953
{}
undefined

If I include queryParser, I can get the GET parameters without a problem, but what I really want is the post parameters either as straight standard params (like my example), or as JSON. I've tried the request both ways.

e.g. This gives the same response in the console:

POST /test?GET_PARAM=def HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 44

{ POST_PARAM1: 'abc', POST_PARAM2: 'ghi' }
Doug
  • 5,243
  • 7
  • 54
  • 89
  • http://stackoverflow.com/questions/22208975/restify-on-node-js-post-body-json – Erdi Sep 14 '15 at 14:43
  • @OsmanErdi Thanks, but req.body is "undefined", if it wasn't I was going to try and parse it a different way. – Doug Sep 14 '15 at 14:53
  • https://www.whatsnoodle.com/handling-posted-data-in-restify-and-node-js/ another one , How did you post in html ? Maybe , you should use server.use(restify.bodyParser({ mapParams: false })); to put values into "req.body" – Erdi Sep 14 '15 at 14:56

1 Answers1

4

I believe I've had the same frustrating problem in the past: A missing Content-Type header. Restify will just ignore any content if the header is not there. Your JSON example should have: Content-Type: application/json

Not sure about your first example, perhaps multipart/form-data see this question.

Also: Your JSON example isn't valid JSON. You need to have double quotes around the keys and the values like so: { "POST_PARAM1": "abc", "POST_PARAM2": "ghi" }

A good tool for interfacing and building requests for this type of stuff is Postman, as it adds those headers automatically when you build the body and choose the type.

Community
  • 1
  • 1
Matt Forster
  • 185
  • 1
  • 6
  • 1
    Thank-you, this was definitely it. I wish I had checked back sooner to this thread. I just figured this out late last night. – Doug Sep 15 '15 at 16:35