用Node js覆盖req.header中设置的内容类型



我有一个web服务向API发出post请求,在这个过程中,由于某种原因,头的内容类型值被从"application/json"覆盖为"text/html"。这导致POST请求失败,因为API只接受内容类型:"application/json"。为了克服这一点,我将让web服务接触代理web服务器,该服务器将实现服务器端代码,将req.header值"content-type"修改回"application/json",并将post请求与req.body和req.headers一起发送到API。我正试图在nodejs(使用express js(中实现这一点。如何覆盖代理节点js服务器上的req.header?我尝试过只使用application/json的accept内容类型,但这并没有达到我所需要的效果:(

let request = require('request');
let proxyRequest = (data,headers)=>{
var options = {
'method': 'POST',
'url': 'your-api-url',
if (headers.hasOwnProperty('Content-Type')){
delete headers['Content-Type']
}

headers['Content-Type']= 'application/json'
body: data
};
request(options, function (error, response) { 
if (error) throw new Error(error);
console.log(response.body);
});

};

最新更新