I have a piece of code for PUT method like this
module.exports.addRoutes = function(server) {
//PUT
server.put('/api/public/place/:id', function(request, response) {
//this is just for testing, please do not care about the id
var placedetail = request.body;
for(var key in placedetail)
{
console.log(placedetail[key]);
}
});
};
In this code, I want to get all the request body field, like request.body.name , request.body.email, etc. The field parameter will be decided by the user , the server will get the value by key.
For example, a user put the data in the request like
{
name:"Test"
email:"abc@gmail.com"
}
another user put the data like
{
address:"Test"
location:"Some Test String"
}
I use this way and got the exception "Object.keys called on non-object while trying to access object". But when access each field , for example: request.body.name, request.body.email , it still get the parameter succesfully.
I also you another to way to parse the data by JSON.parse(request.body.toString()), but actually , the data is not JSON and can not parse.
How can I get all fields of request.body?
Thanks.
You can use body-parser module to parse the req.body data:
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.