I'm trying to create HTTP Interceptor in Angular 4 but I'm having some weird error. Following is my error:
Argument of type 'Observable<Response>' is not assignable to parameter of type 'Observable<Response>'.
Following is my Code:
import { Injectable } from '@angular/core';
import { Http, ConnectionBackend, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import * as _ from 'lodash';
@Injectable()
export class HttpInterceptor extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.request(url, options)); // Here is the error
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.get(url, options)); // Here is the error
}
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.post(url, body, this.getRequestOptionArgs(options))); // Here is the error
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.put(url, body, this.getRequestOptionArgs(options))); // Here is the error
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.delete(url, options)); // Here is the error
}
getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers(); // Here is the error
}
options.headers.append('Content-Type', 'application/json');
return options;
}
intercept(observable: Observable<Response>): Observable<Response> {
return observable.catch((err, source) => {
if (err.status == 401 && !_.endsWith(err.url, 'api/auth/login')) {
this._router.navigate(['/login']);
return Observable.empty();
} else {
return Observable.throw(err);
}
});
}
}
Does anyone know what is going wrong here? I tried debugging for 3 hours but unable to find any clue.
Edit:
I also tried to remove everything and written a code like this:
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, options);
}
But still it's giving same error:
Argument of type 'string | Request' is not assignable to parameter of type 'string | Request'. Type 'Request' is not assignable to type 'string | Request'.
Use this logic to intercept Ajax in any framework.
}
Here is complete example for Angular.
Http interceptor is already implemented in Angular 4.3.4 and is described in the documentation.
You need to implement the
intercept
method ofHttpInterceptor
interface, do something with the request, and call thenext.handle(req)
method.It's also necessary to register interceptor in app's providers section
The following instruction worked for me perfectly:
In the app module:
In the interceptor:
IMPORTANT: http should be an instance of HttpClient!
The globally available DOM typings (
"lib": ["dom"]
in your tsconfig) includeResponse
andRequest
interfaces that are unrelated to the types used by Angular.You need to import
Response
andRequest
from@angular/http
.akn's answer is the right one. I was trying to make it work with http service, but the interceptor only works whit httpClient service.