简体   繁体   中英

Unable to overwrite header attribute Content-type in http calls

Following is the code which sends the request -

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

From the logged httpResponse, I can see the request which was actually sent -

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

I can see the From attribute set to sandeepan@example.com , as I modified it, but I cannot see the Content-type changed to application/json .

Can someone explain? Any pointers?

form option implies application/x-www-form-urlencoded content-type: https://github.com/request/request/blob/master/request.js#L1249

If you are sending json, you need to use json option instead: https://github.com/request/request/blob/master/request.js#L1278

There is no need to set content-type explicitly:

       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
        },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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