以下是发送请求的代码 -
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
。
有人可以解释一下吗?有什么指针吗?
答案 0 :(得分:0)
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
无需明确设置内容类型:
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
},