Angular 5, httpclient ignores set cookie in post

前端 未结 1 856
[愿得一人]
[愿得一人] 2021-01-05 03:24

I\'m dealing with HttpClient in Angular 5, the problem is the cookie sent by the server during login process, it seems that Angular is ignoring it because next request with

相关标签:
1条回答
  • 2021-01-05 04:09

    Finally I was able to made it work!!

    The solution was in post request,

    Maybe I'm wrong but it seems like RequestOptions class was deprecated in Angular 5.0.0, so after several attempts hit the right configuration, Here is the method fixed

    /** POST: add a new hero to the server */
    makeLogin (us:string, password:string): Observable<any> {
        let enco : any = new HttpHeaders()
            .set('Content-Type', 'application/x-www-form-urlencoded');
    
        let body : any = new HttpParams()
        .set('username', us)
        .set('password', password);
        return this.http.post(this.urlServicioLogin,
         body.toString(),
         {
           headers: enco,withCredentials:true
         }
       );
    }
    

    Now with withCredentials true I can see in browsers the cookie being set, and after that send it with subsequent requests.

    Note: If the app has to consume a rest service where auth is made with cookies ALL interactions with backend will need withCredentials flag set to true.

    0 讨论(0)
提交回复
热议问题