[英]how to set content type in “request” npm package
I am sendin a request from node server to some other server , but i need to send content type 我从节点服务器发送请求到其他服务器,但我需要发送内容类型
application/json
应用程序/ JSON
How can i send that , I am using this format 我怎么发送,我使用这种格式
request.post('https://server.com/index.php/rest/V1/integration/admin/token',{form:postData},function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
I am getting error while I am trying this 我在尝试这个时遇到错误
request.post(
'https://server.com/index.php/rest/V1/integration/admin/token',
{
form:postData,
headers:{
"Content-Type": "application/json"
}
},function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
"message":"Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded"
“message”:“服务器无法理解Content-Type HTTP标头媒体类型application / x-www-form-urlencoded”
Please change key name
form
tojson
.请将密钥名称
form
更改为json
。
request.post('https://server.com/index.php/rest/V1/integration/admin/token', {
json: postData
}, function(error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
As per the docs you need to set json:true
for application/json
to be added to the header 根据你需要设置
json:true
的文档 json:true
将application/json
添加到标题中
let data = { foo: "bar" }
request.post({
uri: 'www.domain.com/upload',
body: data,
json:true
}, function (err: any, res: any, body: any) {
//handle callback
});
See the documentation . 请参阅文档 。 You just need to include a value for the
headers
property in the options object: 您只需要在options对象中包含
headers
属性的值:
request.post(
'https://server.com/index.php/rest/V1/integration/admin/token',
{
form: postData, /* Ensure this is a string of JSON! */
headers:{
"Content-Type": "application/json"
}
},
your_callback_function
);
Fixed by this , 由此修复,
request.post({
url: url,
method: "POST",
headers: {
"content-type": "application/json",
},
json: postData
},function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.