How to add Authorization Header to Angular http request?

后端 未结 5 589
悲&欢浪女
悲&欢浪女 2020-12-08 05:03

This is my first post.

I\'ve just started learning Go and Angular and I\'m attempting to connect the angular app to a go api. I\'ve written both and am stuck identi

5条回答
  •  Happy的楠姐
    2020-12-08 05:25

    In case you don't want to add interceptor, this worked for me:

    var header = {
      headers: new HttpHeaders()
        .set('Authorization',  `Basic ${btoa(AuthService.getToken())}`)
    }
    
    this.http.get(url, header)
    

    For Bearer,

    set('Authorization',  `Bearer ${AuthService.getToken()}`)
    

提交回复
热议问题