var restify = require('restify');
var patio = require('patio'),
comb = require('comb');
var DB = patio.createConnection({
host:'localhost',
port:3306,
type:'mysql',
maxConnections:10,
minConnections:1,
user:'root',
password:'azerty',
database:'LoL'
});
var server = restify.createServer();
server.use(restify.bodyParser());
server.post('/update', updateUserInformation);
function updateUserInformation(request, response, next) {
console.log(request.body);
}
server.listen(2100, function () {
console.log('%s listening at %s', server.name, server.url);
});
Here's my code and I really don't know why this code don't work for me. Any idea ?
POST url : http://localhost:2100/update
POST data : user_id=112345
Thank you.
What do you mean "doesn't work"? Can you:
I am having the same problem.
when I post something, my rest server receives a json string and not a json object, so for while I doing the workaround to execute the JSON.parse(req.body) to get the json objects.
I am having the same problem as you cairo. Any solution for this? I don't want to have to do that extra call unless it is necessary
is the client sending as application/json? (I think bodyparser will not parse the body if it is not json in the http headers)
Yes it is. If I don't send it all kinds of errors appear because of what you yourself just mentioned, it doesn't parse it at all.
I am using restify 1.4.4 on Linux - for POST method:
curl -i --data "[email protected]&first_name=John&city=London" http://127.0.0.1:10001/user
My server:
var server = restify.createServer();
server.use(restify.bodyParser());
server.listen(10001);
I am logging req.body =>
[email protected]&first_name=John&city=London
If I remove the use of bodyParser I simply have undefined.
I guess you NEED to JSON.parse the body yourself - unless I misunderstood the documentation - but the single values are present inside params anyway
{
email: '[email protected]',
first_name: 'John',
city: 'London'
}
It works for me.
Can you guys provide more information about this issue?
I am playing around with it to evaluate its use for an API project so I am quite keen to understand how many of these issues are actually woofs
perhaps you may do this
var restify = require('restify');
var server = restify.createServer();
server.use(restify.bodyParser());
server.post("/hello", function(req, res, next){
console.log(req.params);
res.send(200);
res.end();
});
server.listen(10001);
Beware of the position of the _.use_ calls. They must be before the route declaration.
solution worked for me :) :
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
update -> server.use(restify.plugins.bodyParser());
Most helpful comment
Beware of the position of the _.use_ calls. They must be before the route declaration.