user9729328
user9729328

Reputation:

Angular 6 - Deprecated methods

I'm trying to change the request options but it's deprecated. I can't find the option for this.

Any help?

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { HttpHeaders} from "@angular/common/http";
import {RequestOptions} from "@angular/http";

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private baseUrl:string = 'http://localhost:8080/api';
  private headers = new HttpHeaders({'Content-Type':'application/json'});
  private options = new RequestOptions({headers:this.headers});

  constructor() { }
}

Upvotes: 2

Views: 9033

Answers (2)

Shinoy Babu
Shinoy Babu

Reputation: 1769

Deprecated APIs and Features

Headers -> HttpHeaders
RequestOptions -> HttpRequest
HttpModule -> HttpClientModule

Old

import {HttpModule, Headers, RequestOptions} from "@angular/http";

New

import { HttpClientModule, HttpRequest, HttpHeaders } from "@angular/common/http";

Refer the following URL for more details: https://angular.io/guide/deprecations

Upvotes: 1

Benedikt Schmidt
Benedikt Schmidt

Reputation: 2278

I think this is just an import problem, because they moved these options to another place. The documentation states:

use @angular/common/http instead

So I guess you just have to import the options from @angular/common/http instead of @angular/http

EDIT

I should have looked a bit closer. Headers can now be send a bit differently than before, you don't have to use RequestOptions anymore, just pack it up as a simple object. In your case it could look like this:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })
};

Then you can use these options with your http methods. Here's an example from angular's fundamentals:

addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

Upvotes: 3

Related Questions