Node.js 将request.post的内容类型头设置为json
我正在为我的项目使用“hackathonstarter”节点束。在这个构建中,当我尝试从request.post调用API时,所有API都将使用“Content typeNode.js 将request.post的内容类型头设置为json,node.js,api,header,http-post,content-type,Node.js,Api,Header,Http Post,Content Type,我正在为我的项目使用“hackathonstarter”节点束。在这个构建中,当我尝试从request.post调用API时,所有API都将使用“Content type”application/x-www-form-urlencoded;charset=utf-8'头。我尝试从API调用更改头,但只需要 内容类型:“application/x-www-form-urlencoded;charset=utf-8” 所有API的标题。我已经尝试了下面的代码。我想为所有API设置applicatio
”application/x-www-form-urlencoded;charset=utf-8'
头。我尝试从API调用更改头,但只需要
内容类型:“application/x-www-form-urlencoded;charset=utf-8”
所有API的标题。我已经尝试了下面的代码。我想为所有API设置application/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表示形式并添加
内容类型:application/json头。此外,解析
响应主体为JSON
我对你的帮助无话可说,非常感谢(乘以*10K:)。