0

I'm using the latest version (2.6.1) of Restify, and I really don't know what is happening for the Restify not be parsing the data submited on PUT/POST method, it simply doesn't make sense, Restify should be assigning the data to "req.params". I've just lost plenty of time to figured this out and I just didn't, I don't know what is going on. Is this a real issue or do I completely misunderstood the documentation?

So, I execute the follow curl command:

curl -is -X PUT -d "phone=1-800-999-9999" http://localhost:8080/note

And it just returns no params whatsoever, it must be returning the phone value submited on the curl command above:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 4
Date: Wed, 29 Jan 2014 15:14:22 GMT
Connection: keep-alive
"{}"

This is the full code behinded, can't be more simple than this:

var restify = require('restify');
var server = restify.createServer({
  name: 'App',
  version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
server.use(restify.queryParser());
function send(req, res, next) {
  res.send(JSON.stringify(req.params));
  return next();
}
var server = restify.createServer();
server.put('/note', send);
server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

Someone has an answer for this behavior? It really frustrating...

Caio Luts
  • 367
  • 2
  • 14

1 Answers1

1

Try posting to restify as JSON. Like this:

{ "phone" : "1-800-999-9999" }
clay
  • 5,917
  • 2
  • 23
  • 21
  • You mean like this? `curl -is -X PUT -d '{ "phone" : "1-800-999-9999" }' http://localhost:8080/note`. It didn't work, I also tried with `restify.createJsonClient` sending by `client.put('/note', { phone: '1-800-999-9999' }, callback);` and it just didn't parse the data arguments to "req.params". – Caio Luts Jan 29 '14 at 16:19
  • I also tried to specify `-H "Content-Type: application/json"` and didn't work as well. It's so odd, this should be working pretty smoothly. – Caio Luts Jan 29 '14 at 16:25
  • You might need to encapsulate the JSON object: `{"data":{"phone" : "1-800-999-9999"}}`, and then once parsed access it with `req.params.data.phone`. See if that works, and I can update my answer to match. – clay Jan 29 '14 at 16:38
  • I spent a lot of time and the problem was that I was defining the server twice, and in the second definition I wasn't specifying to use queryParser. Pure lack of attention, maybe I'm working too much. Thanks a lot. – Caio Luts Jan 29 '14 at 17:07
  • Ah, I should have seen that too in your question. Glad you have it resolved. – clay Jan 29 '14 at 17:37