<分区>
我有一个用 Go 编写的 POST API 端点,我在 Golang 响应中设置了一个名为 Set-Cookie 的 header 。我已经设置了 cors 并且调用返回了正确的值。问题是响应中的 header 是空的。如果我查看我的网络选项卡,它说我的 Set-Cookie header 正在返回我期望的值,它只是在 Angular 上是空的(请参阅我的 response.headers 和我的网络的 console.log 的屏幕截图选项卡)。
编辑
在 golang 中设置 set-cookie header 后,在后续请求中添加我的 cookie 在 chrome 中的样子的图片。看起来不像 chrome 自动设置 cookie。
编辑
所以,看起来肯定是浏览器问题。当我用 postman 到达终点时,cookie 肯定已设置。我不一定想在angular中获取cookie,我只是想在浏览器中设置cookie。
我的 Angular 代码:
login(user): Subscription {
return this.http
.post(`${SERVICE_URL}/login`, user, {
observe: 'response',
headers: new HttpHeaders({'Content-Type': 'application/json'})
})
.subscribe((response: HttpResponse<any>) => {
console.log(response.headers);
}, (response) => {
this._user$.next(null);
console.log(response);
});
}
我使用 rs/cors 在 golang 服务器中设置 cors
c := cors.New(cors.Options{
AllowedMethods: []string{"GET","POST", "PUT", "OPTIONS"},
AllowedOrigins: []string{"http://localhost:4200", "localhost:4200"},
AllowedHeaders: []string{"X-Requested-With", "Content-Type", "Authorization", "Set-Cookie"},
ExposedHeaders: []string{"Set-Cookie"},
AllowCredentials: true,
})