如何在Angular中保留http代理请求的原始标头



为了避免CORS错误,我在Angular应用程序中配置了一个代理。它位于代理后面的外部url,所以我实现了公司代理配置。

这是我的proxy.conf.js文件

var HttpsProxyAgent = require('https-proxy-agent');
var proxyConfig = [{
context: '/api',
target: '{{external_url}}',
secure: true,
changeOrigin: true,
logLevel: "debug",
pathRewrite: {
"^/api": ""
}
}];
function setupForCorporateProxy(proxyConfig){
var proxyServer = "{{proxyServer}}";
if(proxyServer){
var agent = new HttpsProxyAgent(proxyServer);
proxyConfig.forEach(function(entry){
entry.agent = agent;
});
}
return proxyConfig;
}
module.exports = setupForCorporateProxy(proxyConfig);

请求工作正常,响应是正确的,但是外部url应该回答的头不存在于代理的响应中。这个标头是必要的,因为它包含了一些需要在应用程序内部使用的令牌。

有没有办法将原始标头保留在代理响应中?

angular.json配置`

"serve": {
...
"options": {
"browserTarget": "move-safe:build",
"proxyConfig": "src/proxy.conf.js"
}
}

这就是我在应用程序组件中请求url的方式:

corsRequest(){
this.http.get("/api", {responseType: 'text', observe: 'response'})
.subscribe(res => {
console.log(res); //This is the part where the original headers are not present
});
}

该问题与服务器上配置为";"安全";用";httpsOnly";标志已启用。

将服务器初始化为https所需的标头已按预期返回。

这个答案帮助我弄清楚:Angular 8得到";设置Cookie";HttpResponse 的标头

最新更新