【问题标题】:How to remove cookie param in request header for a post request in angular 6如何在 Angular 6 中删除请求标头中的 cookie 参数以进行发布请求
【发布时间】:2020-09-29 12:05:56
【问题描述】:

当发送一个 post 请求时,cookie 参数被设置在请求标头中。 有没有办法停止在 api 请求标头中发送 cookie 参数 用于角度拦截器中的特定 api 调用

【问题讨论】:

    标签: angular6 angular-http-interceptors request-headers


    【解决方案1】:

    http module 默认不设置 cookie 参数。当您收到响应时,http 模块会丢弃该 cookie,并且不会将其添加到后续请求中。所以你已经手动添加了:

    checkAuth() {
        // if we did not add { withCredentials: true }, response would be false becasue, cookies wont be attached
        return this.http.get<SignedinResponse>(this.rootUrl + '/signedin',{withCredentials:true}).pipe(
          tap(({ authenticated }) => {
            // console.log('response from /signedin', res);
            
          })
        );
      }
    

    您必须手动将{withCredentials:true} 添加到每个请求中。取而代之的是我们编写拦截器,来修改req obj并默认添加cookie。

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 1970-01-01
      • 2016-12-28
      • 2016-06-06
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多