ich mit meiner NodeJS/Koa.js App ein seltsames Problem habe, wo eine HTTP-Anfrage Ich mache mit dieser Fehlermeldung zurückgibt:Knoten Anforderungsmodul nicht Einstellung Content-Type als application/json
{"Message":"The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource."
Jetzt, wenn ich die gleiche Anfrage mit dem Postboten mache, bekomme ich korrekte Ergebnisse zurück, also habe ich gefolgert, dass etwas in meinem Code schief läuft. Ich kann es einfach nicht herausfinden. Hier ist mein Code, um die Anfrage und die Nutzlast zu machen.
// 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);
}
}
});
});
Nutzlast:
{
"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
}
Wie Sie sehen können, habe ich die richtigen Content-Type-Header in meinem Header haben Objekt, das in der Nutzlast I auf die Anfrage-Funktion übergeben, aber es seeems noch, als ob es wird als x-www-form-codiert gesendet. Kann jemand sehen, was hier schief gehen könnte?
Dank
Perfect! Danke – devoncrazylegs