我正在为我的项目使用“hackathon-starter”节点群。在此版本中,当我尝试从 request.post 调用 API 时,它将采用“所有 API 的内容类型
'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)
});
控制台上出现我的错误消息。
{“时间戳”:1484822264270,“状态”:415,“错误”:“不支持的媒体类型”,“异常”:“org.springframework.web.HttpMediaTypeNotSupportedException”,“消息”:“内容类型'application / x- www-form-urlencoded;charset=utf-8' 不支持","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 - 将正文设置为值的 JSON 表示形式并添加 内容类型:application/json 标头。另外,解析 JSON 格式的响应正文。