为request.post设置内容types的标题为json
我正在为我的项目使用“hackathon-starter”节点群。 在这个构build中,当我尝试从request.post调用一个API时,它将采用“内容types'application/x-www-form-urlencoded;charset=utf-8'
打电话,但只会采取
内容types:'application / x-www-form-urlencoded; charset = utf-8'
所有API的头。 我已经尝试下面的代码。 我想为所有API设置应用程序/ json 。
var querystring = require('querystring'); var request = require('request'); var form = { "userType": req.body.type, "userName": req.body.mobile, "email": req.body.email, "name": req.body.name, "password": req.body.password }; var formData = querystring.stringify(form); var contentLength = formData.length; request.post({ headers: {'content-type':'application/json'}, url:'mylink', form: formData // I have tried form as well. },function(error, response, body){ console.log(body) });
我在控制台上的错误消息。
{"timestamp":1484822264270,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=utf-8' not supported","path":"mylink"}
我想你需要使用json
选项,而不是根据您的要求:
var form = { "userType": req.body.type, "userName": req.body.mobile, "email": req.body.email, "name": req.body.name, "password": req.body.password }; request.post({ url:'mylink', json: form, },function(error, response, body){ console.log(body) });
从选项文件:
json – 将body设置为值的JSON表示,并添加Content-type:application / json头。 此外,将响应正文parsing为JSON。