【发布时间】:2017-09-20 10:17:43
【问题描述】:
我的 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 形式发送的。谁能看到这里可能出了什么问题?
谢谢
【问题讨论】:
标签: json node.js http post request