Angular 6 - httpClient passing basic auth in httpOptions

前端 未结 6 1370
灰色年华
灰色年华 2020-12-08 10:48

I have a service in Angular 6 and I\'m trying to change a record but it\'s saying I\'m not authorized.

Right now I have this:

const httpOptions = {
          


        
相关标签:
6条回答
  • 2020-12-08 11:27
    const httpOptions = {
      headers: new HttpHeaders(
        {
          'Content-Type': 'application/json',
          'Authorization': `Basic ` + btoa('user:password'),
        }
      )
    };
    
    
    return this.http.post<any>(
          `apilink`,{},
          httpOptions
        ).pipe(map(res => {
          return res;
    }));
    
    0 讨论(0)
  • 2020-12-08 11:28

    You can add basic authorization by appending it in headers, as below:

    var headers_object = new HttpHeaders();
    headers_object.append('Content-Type', 'application/json');
    headers_object.append("Authorization", "Basic " + btoa("username:password"));
    
    const httpOptions = {
      headers: headers_object
    };
    
    0 讨论(0)
  • 2020-12-08 11:30

    httpClient passing basic auth in httpOptions is different in Angular 6

    let httpHeaders= new HttpHeaders();
    httpHeaders.append('Content-Type', 'application/json');
    httpHeaders.append("Authorization", "Basic " + btoa("username:password"));
    
    const httpOptions = {
      headers: httpHeaders
    };
    
    update(id, title, content) {
        const updateData = { id: id, title: title, content: content };
          return this.http.put(`http://myurl/${id}`, updateData, httpOptions);
      }
    
    0 讨论(0)
  • 2020-12-08 11:37

    Looking at the angular.io documentation, it's pretty straightforward.

    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': 'Basic my-auth-token'
      })
    };
    

    And you can use the httpOptions constant as you did.

    For more information: https://angular.io/guide/http#adding-headers

    0 讨论(0)
  • 2020-12-08 11:40

    Just add your token/authorization in the headers like this -

    let httpHeaders = new HttpHeaders()
                  .set('authorization', this.authorizationHeaderValue)
                  .set('Content-Type', application/json); 
    

    Both have methods such as set and append. set constructs a new body with a new value and append constructs a new body with an appended value

    PS: Here I am assuming the variable (this.authorizationHeaderValue) value is included value like Bearer or Basic or whatever needed, Change it accordingly.

    For more read here

    • https://www.concretepage.com/angular-2/angular-httpclient-get-example#parameters
    0 讨论(0)
  • 2020-12-08 11:48
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    
    var headers = new HttpHeaders();
    
    var token = localStorage.getItem('token');
    
    headers.append('Content-Type', 'application/json');
    
    headers.append("Authorization", "Basic " + token));
    
    const httpOptions = {
      headers: headers
    };
    
    0 讨论(0)
提交回复
热议问题