繁体   English   中英

无法覆盖HTTP调用中的标题属性Content-type

[英]Unable to overwrite header attribute Content-type in http calls

以下是发送请求的代码-

var requestData = {
            "trigger_id":global.config.primusConfig.trigger_id,
            //"mobile":global.config.primusConfig.trigger_id,
            "email": global.config.primusConfig.email,
            "params":{
                "Amount":"200"
            }
            /*,
            "files":{
                    “sample.pdf” : fileData
            }*/
            };

            request({
                headers: {
                'cid': global.config.primusConfig.client_id,
                'datetime': datetime,
                'hash': hash,
                'Content-type': 'application/json',
                'From':'sandeepan@example.com'
              },
                url: global.config.primusConfig.apiEndPoint,
                method: 'POST',
                form: requestData,
                body : req.rawBody,

            },
            function(error,httpResponse,body) {
                console.log("Response handling part "+global.config.primusConfig.apiEndPoint+" formdata "+JSON.stringify(requestData));
                if(error) {
                    console.log("Error "+error);
                }
                else {
                    console.log("Not error case- Body: "+body+" httpResponse:"+JSON.stringify(httpResponse));
                    callback();
                }
            });

从记录的httpResponse中,我可以看到实际发送的请求-

 httpResponse:{"statusCode":500,"body":"\n  <h1> 500 - Internal server error </h
1>\n  <h2>  </h2>\n","headers":{"content-type":"text/html; charset=utf-8","date"
:"Tue, 31 Oct 2017 15:19:13 GMT","etag":"W/\"38-3b8a0f21\"","x-powered-by":"Expr
ess","content-length":"56","connection":"Close"},"request":{"uri":{"protocol":"h
ttps:","slashes":true,"auth":null,"host":"domain.com","port":4
43,"hostname":"domain.com","hash":null,"search":null,"query":n
ull,"pathname":"/sendnotification","path":"/sendnotification","href":"domain.com/sendnotification"},"method":"POST","headers":{"cid":"
19","datetime":"2017-10-31 00:00:00","hash":"88072482e5125ed69f28ce60af859653049
9e11d","Content-type":"application/x-www-form-urlencoded","From":"sandeepan@exam
ple.com","content-length":70}}}

修改后,我可以看到From属性设置为sandeepan@example.com ,但是看不到Content-type更改为application/json

有人可以解释吗? 有指针吗?

form选项暗含application/x-www-form-urlencoded内容类型: https : //github.com/request/request/blob/master/request.js#L1249

如果要发送json,则需要改用json选项: https : //github.com/request/request/blob/master/request.js#L1278

无需显式设置content-type:

       request({
            headers: {
            'cid': global.config.primusConfig.client_id,
            'datetime': datetime,
            'hash': hash,
            'From':'sandeepan@example.com'
          },
            url: global.config.primusConfig.apiEndPoint,
            method: 'POST',
            json: requestData
        },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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