I have been working with angular 6.0.0. I have a REST API with basic auth and it works fine on Postman but it gives an error in my service as below
401 (Unauthorized)
Here is my code:-
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import { struct_paket } from './data';
@Injectable({
  providedIn: 'root'
})
export class AppserviceService {
  constructor(private http: Http) { }
  auth(headers: Headers) {
    headers.append('Authorization', 'Basic ' +
      btoa('admin:310b5678eece63336e4698d2722aad91'));
  }
  getpaket() {
    let headers = new Headers();
    this.auth(headers);
    return  this.http.get('www.example.com/api', {
      headers: headers
    }).map(res => res.json() as struct_paket );
  }
}
UPDATE
service.ts
export class AppserviceService {
constructor(private http: HttpClient) {}
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization:
        'Basic YWRtaW46MzEwYjU2NzhlZWNlNjMzMzZlNDY5OGQyNzIyYWFkOTE='
    })
  };
  get() {
    return this.http.get<struct_paket>('http:www.example.com/api', this.httpOptions)
  }
}
and call it in component.ts
this._service.get()
    .subscribe(x=>
      {
        this.Pakets = x
      },error =>{console.log(error);}
    );

