简体   繁体   中英

How do I send my request body as text using $http.post?

Also using npm's body-parser, how could I send the data in

$http.post('/',data)

so that when I receive it with

app.route('/').post(function(req,res){
  var body_better_be_a_string = typeof req.body;
})

req.body should be a string type. Have tried but req.body always comes back as [object Object], need req.body to be a string.

JSON.stringify(req.body); will give you a string The reson being is im guessing the req.body is a json object and when the [object Object] appears you are trying the view it. If you use JSON.stringify it will be parsed to string

The Express docs specify that the data sent in req.body is a list of key value pairs, so if, for example, you are sending 'application/json' you could use JSON.stringify(req.body) to ensure that it is of type string for further manipulations: http://expressjs.com/en/api.html#req.body

Additionally, it might help if you updated your questions with the reason "why" you need it to be a string. Is there some manipulation you are trying to implement that isn't working?

Also, you could include the other parts of your Node script that use the body-parser since those may be relevant to the answer.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM