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'

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题