0

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);}
    );
4
  • which version of angular are you used? Commented Jul 7, 2018 at 3:17
  • angular/cli version : "6.0.8" @baj9032 sorry forgot about that Commented Jul 7, 2018 at 3:22
  • check my answer it would be helpful. Commented Jul 7, 2018 at 3:43
  • any help please ? Commented Jul 9, 2018 at 5:53

2 Answers 2

1

Update headers after appending

Try

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 {
  headers : Headers;
  constructor(private http: Http) { }
  auth() {
    this.headers = this.headers.append('Authorization', 'Basic ' +
      btoa('admin:310b5678eece63336e4698d2722aad91'));
  }

  getpaket() {
    this.headers = new Headers();
    this.auth();

    return  this.http.get('www.example.com/api', {
      headers: headers
    }).map(res => res.json() as struct_paket );


  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

its not working type 'void' is not assignable to type 'Headers'
1

Headers is now moved to '@angular/common/http' and name is also changed Headers to HttpHeaders.

Example:

import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class TestService {
  constructor(private http: HttpClient) {}

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization':'Basic' +  btoa('admin:310b5678eece63336e4698d2722aad91'))
    })
  };
  call() {
    return this.http
      .get<struct_paket>('www.example.com/api', this.httpOptions)
  }
}

5 Comments

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Basic YWRtaW46MzEwYjU2NzhlZWNlNjMzMzZlNDY5OGQyNzIyYWFkOTE=' }) }; return this.http.get('www.example.com/api', { headers: httpOptions }).map(res => res.json() as struct_paket ); but i got error argument of type {headers : httpHeaders;} is not assignable to parameter of type requestOptionsArgs
change constructor(private http: Http) { } to constructor(private http: HttpClient) { } and import HttpClient from import { HttpClient } from '@angular/common/http';
still got error like this argument of type {headers : httpHeaders;} is not assignable to parameter of type requestOptionsArgs
I have Updated my ans.
still get error 401 i call service funtion like this `this._service.call() .subscribe(x=> { this.Pakets = x },error =>{console.log(error);} );'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.