RESTify on Node.js POST body / json

I need help. I am sending json data to node server. The node server uses RESTify for its API. I'm having trouble getting req.body.name from the body of the published data.

Published data contains json body. In it, I have keys such as name, date, address, email address, etc.

I want to get the name from json body. I am trying to make req.body.name , but it does not work.

I also included server.use(restify.bodyParser()); and does not work.

I can req.params.name and assign a value. But if I POST json data like: {'food': 'ice cream', 'drink' : 'coke'} , I get undefined. However, if I do req.body , I get the full json body message. I want to be able to specifically get an item like "drink", and have this show on the .log console.

 var restify = require('restify'); var server = restify.createServer({ name: 'Hello World!', version: '1.0.0' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.jsonp()); server.use(restify.bodyParser({ mapParams: false })); server.post('/locations/:name', function(req, res, next){ var name_value = req.params.name; res.contentType = 'json'; console.log(req.params.name_value); console.log(req.body.test); }); server.listen(8080, function () { console.log('%s listening at %s', server.name, server.url); }); 
+9
json restify
source share
5 answers

If you want to use req.params , you must change:

 server.use(restify.bodyParser({ mapParams: false })); 

use true:

 server.use(restify.bodyParser({ mapParams: true })); 
+8
source share

Have you tried using the standard JSON library to parse the body as a json object? Then you can grab everything you need.

 var jsonBody = JSON.parse(req.body); console.log(jsonBody.name); 
+4
source share

In addition to the answer below. The latest syntax in update 5.0 was a change.

The whole parser you are looking for is inside restify.plugins instead of restify use restify.plugins.bodyParser

The method of using it is this.

 const restify = require("restify"); global.server = restify.createServer(); server.use(restify.plugins.queryParser({ mapParams: true })); server.use(restify.plugins.bodyParser({ mapParams: true })); server.use(restify.plugins.acceptParser(server.acceptable)); 
+2
source share

you should use req.params with active bodyParser.

 var restify = require('restify'); var server = restify.createServer({ name: 'helloworld' }); server.use(restify.bodyParser()); server.post({path: '/hello/:name'}, function(req, res, next) { console.log(req.params); res.send('<p>Olรก</p>'); }); server.get({path: '/hello/:name', name: 'GetFoo'}, function respond(req, res, next) { res.send({ hello: req.params.name }); return next(); }); server.listen(8080, function() { console.log('listening: %s', server.url); }); 
0
source share
 var restify = require('restify') const restifyBodyParser = require('restify-plugins').bodyParser; function respond(req, res, next) { console.log(req.body) const randomParam = req.body.randomParam res.send(randomParam); next(); } var server = restify.createServer(); server.use(restifyBodyParser()); server.post('/hello/:name', respond); server.head('/hello/:name', respond); server.listen(8080, function() { console.log('%s listening at %s', server.name, server.url); }); 

... This is what works for me with Restify version 8.3.2

0
source share

All Articles