Pri*_*jan 3 api content-type header http-post node.js
我正在为我的项目使用"hackathon-starter"节点束.在此构建中,当我尝试从request.post调用API时,它将采用" 'application/x-www-form-urlencoded;charset=utf-8'所有API的内容类型标头.我已尝试从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)
});
Run Code Online (Sandbox Code Playgroud)
我在控制台上的错误消息.
{"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"}
Run Code Online (Sandbox Code Playgroud)
我猜您需要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)
});
Run Code Online (Sandbox Code Playgroud)
从期权文件:
json - 将body设置为值的JSON表示,并添加Content-type:application/json标头.另外,将响应主体解析为JSON.
| 归档时间: |
|
| 查看次数: |
5106 次 |
| 最近记录: |