Order of angular token

0

How to place my Authorization token in my http request (GET). This is my current code:

import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
    title = 'Guilherme';
    url = 'https://URL.com'
    token = 'b7ff1...';

    constructor(http: Http) {       
        let stream = http.get(this.url, {
            headers: new HttpHeaders().set('Authorization', this.token),
        }).subscribe();
    }
}
  

webpack: Compiled successfully. ERROR in src / app / app.component.ts (28,36): error TS2345: Argument of type 'headers: HttpHeaders;}' is not assignable to parameter of type 'RequestOptionsArgs'

     

Types of property 'headers' are incompatible. Type 'HttpHeaders' is not assignable to type 'Headers'. Property 'forEach' is missing in type 'HttpHeaders'.

    
asked by anonymous 23.01.2018 / 23:39

1 answer

0

I think your problem is happening because you are importing http of a module ( @angular/http ) while Headers comes from another module ( @angular/common/http ).

As an indication of the Angular team (due to the future removal of the @ angular module / http) you should import both from the @angular/common/http module, this should solve your problem.

import { Component } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
    selector: 'app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
    title = 'Guilherme';
    url = 'https://URL.com'
    token = 'b7ff1...';

    constructor(http: HttpClient) {
        let stream = http.get(this.url, {
            headers: new HttpHeaders().set('Authorization', this.token),
        }).subscribe();
    }
}
    
24.01.2018 / 11:19