admin管理员组文章数量:1394544
I am using "hackathon-starter" node bunch for my project. In this build when I try to call a API from request.post it will take "Content type 'application/x-www-form-urlencoded;charset=utf-8'
header for all API. I have tried to change header from API calling but it will take only
Content type : 'application/x-www-form-urlencoded;charset=utf-8'
header for all API. I have tried below code. I want to set application/json for all API.
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)
});
My error message on console.
{"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"}
I am using "hackathon-starter" node bunch for my project. In this build when I try to call a API from request.post it will take "Content type 'application/x-www-form-urlencoded;charset=utf-8'
header for all API. I have tried to change header from API calling but it will take only
Content type : 'application/x-www-form-urlencoded;charset=utf-8'
header for all API. I have tried below code. I want to set application/json for all API.
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)
});
My error message on console.
Share Improve this question edited Dec 9, 2023 at 14:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 19, 2017 at 10:46 Pritesh MahajanPritesh Mahajan 5,1749 gold badges43 silver badges65 bronze badges{"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"}
1 Answer
Reset to default 7I guess you need to use json
option instead based on your requirements:
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)
});
From the options documentaion:
json - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.
本文标签: javascriptSet contenttype header to json for requestpostStack Overflow
版权声明:本文标题:javascript - Set content-type header to json for request.post - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744099414a2590806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论