RESTify on Node.js POST body / json

北城余情 提交于 2019-12-19 05:29:12

问题


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);
});

回答1:


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 }));



回答2:


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);



回答3:


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));



回答4:


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




回答5:


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);
});


来源:https://stackoverflow.com/questions/22208975/restify-on-node-js-post-body-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!