RESTify on Node.js POST body / json
Asked Answered
C

5

13

I am in need of help. I am POSTing json data to my node server. The node server is using RESTify for its API. I am having trouble getting req.body.name from the body of the posted data.

The posted data contains a json body. In it i have keys such as name, date, address, email, etc.

I want to get the name out of the json body. I am trying to do req.body.name but it is not working.

I have also included server.use(restify.bodyParser()); and it is not working.

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

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);
});
Coloratura answered 5/3, 2014 at 20:53 Comment(3)
whats the value of req.body.test ?Venlo
if thats the case you should apply Content-Type: application/json to request headers so restify can do that automatically.Venlo
@Phoenix you should add that as an answer so that I could upvote it. Works like a charm.Bassarisk
B
11

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

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

to use true:

server.use(restify.plugins.bodyParser({ mapParams: true }));
Bethel answered 7/1, 2015 at 18:9 Comment(0)
E
5

Have you tried using the standard JSON library to parse the body as a json object? Then, you should be able to grab whatever property you need.

var jsonBody = JSON.parse(req.body);
console.log(jsonBody.name);
Erigena answered 4/12, 2014 at 20:51 Comment(0)
B
4

In addition to below answer . The latest syntax in restify 5.0 has been change .

All the parser that you are looking for is inside restify.plugins instead of restify use restify.plugins.bodyParser

The method to use 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));
Babu answered 23/9, 2017 at 17:14 Comment(2)
No need to require 'restify-plugins', all of them are already in restify.plugins.Granadilla
yes it is now in restify again. I will update my answer .Babu
A
1
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);
});

... Is what worked for me with restify version 8.3.2

Ariose answered 12/5, 2019 at 2:14 Comment(0)
V
0

you must use req.params with bodyParser active.

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);
});
Vicinity answered 14/3, 2016 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.