dongtuo7364 2018-10-04 22:04
浏览 77

未在POST响应上设置标头[重复]

This question already has an answer here:

I've got a POST API endpoint written in Go, and I'm setting a header called Set-Cookie in the Golang response. I've set up cors and the call is returning the correct values. The problem is that the headers in the response are empty. If I look at my network tab, it says that my Set-Cookie header is being returned with the value I'm expecting, it's just in angular that it's empty (see screenshots of the console.log of my response.headers and my network tab).

EDIT

adding a picture of what my cookies look like in chrome on subsequent requests after te Set-Cookie header has been set in golang. Doesn't look like chrome auto sets the cookies.

EDIT

So, it definitely seems like it's a browser issue. When I hit the endpoint with postman the cookie is definitely set. I don't necessarily want to get the cookie in angular, I just want to set the cookie in the browser.

enter image description here

My angular code:

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

my cors settings in the golang server using rs/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,
    })

enter image description here

enter image description here

</div>
  • 写回答

2条回答 默认 最新

  • dourang6423 2018-10-04 22:42
    关注

    The browser (looks like chrome devtools) will handle the cookie for you. If you go looking in the application tab of the devtools you can find the cookies. If you need to get the value of that header in Angular, I suggest you use a different header. One that the browser doesn't intercept. It is good practise to namespace your header like x-my-header.

    There isn't an api in Angular for manipulating the cookies, but there are packages out there that enable it like 'ngx-cookie-service'

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘