[英]Node request module not setting Content-Type as application/json
我的NodeJS / Koa.js应用出现一个奇怪的问题,我正在发出的HTTP请求返回此错误消息:
{"Message":"The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource."
现在,当我使用邮递员发出相同的请求时,我会得到正确的结果,因此我推断出我的代码中有什么问题。 我似乎无法弄清楚。 这是我发出请求和有效负载的代码。
// Content Type
if(options.contentType === 'json') {
headers['Content-Type'] = 'application/json';
}
// Content Length
if(options.contentLength) {
reqHeaders['Content-Length'] = options.contentLength
}
if(headers) {
for(let key in headers) {
if(!headers.hasOwnProperty(key)) {
continue;
}
reqHeaders[key] = headers[key];
}
}
const payload = {
headers : reqHeaders,
url : url,
method : requestType,
timeout : 10000,
form : vars,
followRedirect: true,
maxRedirects: 10,
body : '' || options.body
};
return new Promise(function(resolve, reject) {
request(payload, function(error, response, body) {
if(response) {
if(!error && response.statusCode === 200) {
resolve(response, body);
} else {
if(response.statusCode === 401) {
console.log('token expired');
}
reject(response, body);
}
}
});
});
有效负载:
{
"headers": {
"Cookie": "XDEBUG_SESSION=PHPSTORM",
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkZWdvdWxkLWxvZ2luLmRldiIsImFjY291bnQiOiI1OTY3NmFmZmYyOWE1NWI2MTViOWFiMWEiLCJhdXRoTGV2ZWwiOjAsImlhdCI6MTUwNTg5OTQ3MX0.r-XaeTsQTjSkab9SNjrHgnh6lrgNP0uJCaDIV22A6gM",
"Content-Type": "application/json"
},
"url": "http://54.***.***/api/Report/History",
"method": "POST",
"timeout": 10000,
"form": {
"AccountId": "59676afff29a55b615b9ab1a",
"StartDate": "2017-09-19T10:11:47.0266607+00:00",
"EndDate": "2017-09-19T10:11:47.0266607+00:00",
"VIN": "SALLAK"
},
"followRedirect": true,
"maxRedirects": 10
}
如您所见,我的标头对象中有正确的Content-Type标头,该标头位于传递给请求函数的有效负载中,但仍然看起来像是按x-www-form-encoded发送。 有人可以看到这里出什么问题吗?
谢谢
该文档阅读:
- form-传递对象或查询字符串时,会将主体设置为值的查询字符串表示形式,并添加Content-type:application / x-www-form-urlencoded标头。
和
- json-将主体设置为值的JSON表示,并添加Content-type:application / json标头。
您正在使用form
,因此它会覆盖标头。 尝试改用json
。 无论如何,它将覆盖您的标头,但该值将为'application / json',这应该可以。
如果可以的话,我有几点建议:
json: true
将有助于放置正确的content-type标头。 如果要返回承诺,请考虑使用启用了fullResponse
request-promise来检查令牌到期。
if(headers) { Object.assign(reqHeaders, headers); } const payload = { headers: reqHeaders, url: url, method: requestType, timeout: 10000, json: true, followRedirect: true, maxRedirects: 10, body: options.body || {}, fullResponse: true }; return rp(payload).then(function(response) { if (response.statusCode === 401) { throw Error('Token expired'); } return response.body; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.