0

I have a service like this:

   import { Injectable } from "@angular/core";
import { Http, Response, Headers, RequestOptions } from "@angular/http";
import "rxjs/add/operator/map";
import { Usuario } from "../_models/usuarios";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import { User } from "../_models/user.model";


 private serviceUrl = "apiUrl";
  headers = new Headers({
    Authorization:
      "Bearer " + JSON.parse(localStorage.getItem("currentUser")).token,
    "Content-Type": "application/json"
  });
  options = new RequestOptions({ headers: this.headers });

constructor(private http: Http, private httpClient: HttpClient) {}

  getUser(): Observable<User[]> {
    return this.httpClient.get<User[]>(this.serviceUrl);
  }

Problem is when I try to send options to get method like:

getUser(): Observable<User[]> {
    return this.httpClient.get<User[]>(this.serviceUrl, this.options);
  }

It returns:

message: 'Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'.'

I try to change it using http instead httpClient like:

 return this.http.get<User[]>(this.serviceUrl, this.options);

But now I get

message: 'Expected 0 type arguments, but got 1.'

What am I doing wrong?

2 Answers 2

0

You can use HttpClient it is from @angular/common/http something like this:

import { Injectable } from "@angular/core";
import { Usuario } from "../_models/usuarios";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable } from "rxjs";
import { User } from "../_models/user.model";

@Injectable()
export class UserService{
private serviceUrl = "apiUrl";
  headers = new HttpHeaders({
    Authorization: "Bearer " + JSON.parse(localStorage.getItem("currentUser")).token,
    "Content-Type": "application/json"
  });

constructor(private httpClient: HttpClient) {}

  getUser(): Observable<User[]> {
    return this.httpClient.get<User[]>(this.serviceUrl, { 'headers': this.headers});
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

if you use httpClient, you should

import { HttpHeaders } from '@angular/common/http';

httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': "Bearer " + JSON.parse(localStorage.getItem("currentUser")).token
    })
}

...

getUser(): Observable<User[]> {
    return this.httpClient.get<User[]>(this.serviceUrl,httpOptions);
}

Comments

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.