对于 request.post,将 content-type header 设置为 json
Set content-type header to json for request.post
我正在为我的项目使用 "hackathon-starter" 节点束。在此版本中,当我尝试从 request.post 调用 API 时,所有 API 都将采用“内容类型 'application/x-www-form-urlencoded;charset=utf-8'
header”。我已尝试更改 header 来自 API 呼叫,但只需要
Content type : 'application/x-www-form-urlencoded;charset=utf-8'
header 所有 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 - sets body to JSON representation of value and adds
Content-type: application/json header. Additionally, parses the
response body as JSON.
我正在为我的项目使用 "hackathon-starter" 节点束。在此版本中,当我尝试从 request.post 调用 API 时,所有 API 都将采用“内容类型 'application/x-www-form-urlencoded;charset=utf-8'
header”。我已尝试更改 header 来自 API 呼叫,但只需要
Content type : 'application/x-www-form-urlencoded;charset=utf-8'
header 所有 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 - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.