1

I'm trying to access an API and hitting a brick wall...repeatedly. I've read through the Angular documentation but I'm still missing something. Please advise. Thank you!

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, catchError, tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class RestService {
  private url = 'https://api.com';
  private auth = ('xxxx:xxxx');

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Content-Encoding': 'none',
      'Authorization': 'Basic' + btoa(this.auth)

    })
  };

  constructor(private http: HttpClient) {
  }

  index() {
    return this.http.get<any[]>(this.url, this.httpOptions);
  }

}
  • Same question https://stackoverflow.com/questions/45894628/angular-4-3-httpclient-basic-authorization-not-working – Vu Luu Feb 25 '19 at 02:53
  • I beleive the problem is in Authorization, put a space after Basic – Ricardo Feb 25 '19 at 02:59
  • Thank you. I'm reading through it and see the similarity but I'm using a newer version of Angular and I'm not quite seeing my error. – Rob Thompson Feb 25 '19 at 03:16
  • @Ricardo - that was it. I've been working on this for 4 days. I told my wife it was probably a space or a comma somewhere. Dang if that wasn't true. THANK YOU. You, too, vuluu – Rob Thompson Feb 25 '19 at 03:18

1 Answers1

0

On your authorization header, create a space between the word Basic and your auth key. Like this:

'Authorization': 'Basic ' + btoa(this.auth)

Seloka
  • 345
  • 1
  • 6
  • 20