简体   繁体   English

节点请求模块未将Content-Type设置为application / json

[英]Node request module not setting Content-Type as application/json

I am having a strange issue with my NodeJS/Koa.js app where an HTTP request I am making is returning with this error message: 我的NodeJS / Koa.js应用出现一个奇怪的问题,我正在发出的HTTP请求返回此错误消息:

{"Message":"The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource."

Now, when I make the same request using postman I get correct results back so I have deduced that something is awry in my code. 现在,当我使用邮递员发出相同的请求时,我会得到正确的结果,因此我推断出我的代码中有什么问题。 I just can't seem to figure it out. 我似乎无法弄清楚。 Here is my code to make the request and the payload. 这是我发出请求和有效负载的代码。

 // 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);
                    }
                }
            });
        });

Payload: 有效负载:

{
  "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
}

As you can see, I have the correct Content-Type headers in my headers object that is in the payload I pass to the request function but it still seeems as if it is sending as x-www-form-encoded. 如您所见,我的标头对象中有正确的Content-Type标头,该标头位于传递给请求函数的有效负载中,但仍然看起来像是按x-www-form-encoded发送。 Can anyone see what may be going wrong here? 有人可以看到这里出什么问题吗?

Thanks 谢谢

The docs read: 文档阅读:

  • form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type: application/x-www-form-urlencoded header. form-传递对象或查询字符串时,会将主体设置为值的查询字符串表示形式,并添加Content-type:application / x-www-form-urlencoded标头。

and

  • json - sets body to JSON representation of value and adds Content-type: application/json header. json-将主体设置为值的JSON表示,并添加Content-type:application / json标头。

You are using form , so it overwrites the header. 您正在使用form ,因此它会覆盖标头。 Try to use json instead. 尝试改用json It will overwrite your header anyway, but the value will be 'application/json' which should be okay. 无论如何,它将覆盖您的标头,但该值将为'application / json',这应该可以。

a couple of suggestions if I may: 如果可以的话,我有几点建议:

  • You can use Object.assign to set the headers in the object. 您可以使用Object.assign设置对象中的标题。
  • Setting json: true will take care of putting the right content-type header. 设置json: true将有助于放置正确的content-type标头。
  • You shouldn't have to set the content-length manually, it's a tricky thing to do. 您不必手动设置content-length,这是一件棘手的事情。 Let request deal with that. 让请求处理。
  • If you want to return a promise, consider using request-promise with fullResponse enabled to check for token expiration. 如果要返回承诺,请考虑使用启用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM