angular-http-interceptors

Angular Interceptor route to a different path based on response

痞子三分冷 提交于 2019-12-01 10:49:21
问题 I'm playing around with Angular interceptor and i wanted to re route if the response status is 401 and below is what i've tried . @Injectable() export class AuthInterceptorService implements HttpInterceptor { constructor(private _localStorageService: LocalStorageService, private _router: Router, private _location: Location) { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { if (req.url.includes("/login")) { return next.handle(req); } let authToken:

res.json() is a not a function in HttpClient Angular 2

倾然丶 夕夏残阳落幕 提交于 2019-12-01 06:11:28
问题 I was using Angular Http module before and the method res.json() used to work fine. I have recently tried HttpClient but then the res.json() dosen't seem to work . only using res works can some one tell me what changed has happened in the http client. return this.client.get('https://swapi.co/api/people/1/') .map((res:Response) => { return res.json(); // using maps to filter data returned form the http call this json dosn't work with http client }).map(data => { return data; // using maps of

Angular 4.3 Interceptors for Lazy Loaded Modules

可紊 提交于 2019-12-01 03:26:47
What is the best practice to use core module service in lazy loaded feature module and feature child modules. As per Angular style guide I have the following app -core - core.module.ts -logger.service.ts -token-interceptor.service.ts -authentication.service.ts -shared -shared.module.ts -base module (my feature base , lazy loaded with router-outlet) -base.module.ts -base.routing.module.ts -base -base.component.ts -admin (lazy loaded , child module of base module) -admin.module.ts -admin.routing.ts -admin-component.ts -report(lazy loaded , child module of base module, sibling of admin) -report

How to add multiple headers in Angular 5 HttpInterceptor

依然范特西╮ 提交于 2019-12-01 02:59:50
I'm trying to learn how to use HttpInterceptor to add a couple of headers to each HTTP request the app do to the API. I've got this interceptor: import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; @Injectable() export class fwcAPIInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const authReq = req.clone({ headers: req.headers.set('Content-Type', 'application/json') }); console.log(

HTTP interceptor getting status 0 on failed request using Angular 4

ぃ、小莉子 提交于 2019-12-01 01:30:28
I have following implementation of HTTP interceptors with Angular ^4.3.6 . import {Injectable} from "@angular/core"; import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse } from "@angular/common/http"; import {Observable} from "rxjs/Observable"; import "rxjs/add/operator/do"; @Injectable() export class InterceptorService implements HttpInterceptor { intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { return next.handle(req).do(evt => { console.log(evt);//this logs the success message properly if (evt instanceof

Angular5 HttpInterceptor depending on the result of an Observable

断了今生、忘了曾经 提交于 2019-11-30 22:44:56
I am trying to implement using Angular5 an HttpInterceptor to inject an Authorization header in all HTTP requests. I rely on a third party library (ADAL, here called AuthService ) that exposes a acquireToken() method to get the token to be used for Bearer authorization. The problem is that aquireToken() returns an observable, and i have to subscribe to get the real string I need. Therefore, my code never injects the header, i suppose because next.handle() is executed before acquireToken() returns any value. How can i ensure that the next handler is called only after the token has been

Loader Using HttpClient Interceptor Angular 4

浪子不回头ぞ 提交于 2019-11-30 22:39:44
How can i implement a loader using Angular 4 HttpClient Interceptor? I have done the login and attaching the authorization header and i wonder if i can also do the "loader function" if some data is loading in the process? I would like to attached a icon loader that signals if some data is loading. Here's my authinterceptor below by the way. export class GithubAuthInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const authReq = req.clone({ headers: req.headers.set('Authorization', 'token') }); return next.handle(authReq)

HTTP interceptor getting status 0 on failed request using Angular 4

孤街浪徒 提交于 2019-11-30 20:06:18
问题 I have following implementation of HTTP interceptors with Angular ^4.3.6 . import {Injectable} from "@angular/core"; import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse } from "@angular/common/http"; import {Observable} from "rxjs/Observable"; import "rxjs/add/operator/do"; @Injectable() export class InterceptorService implements HttpInterceptor { intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { return next.handle

Loader Using HttpClient Interceptor Angular 4

与世无争的帅哥 提交于 2019-11-30 17:29:30
问题 How can i implement a loader using Angular 4 HttpClient Interceptor? I have done the login and attaching the authorization header and i wonder if i can also do the "loader function" if some data is loading in the process? I would like to attached a icon loader that signals if some data is loading. Here's my authinterceptor below by the way. export class GithubAuthInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const

How to add multiple headers in Angular 5 HttpInterceptor

无人久伴 提交于 2019-11-30 16:29:13
问题 I'm trying to learn how to use HttpInterceptor to add a couple of headers to each HTTP request the app do to the API. I've got this interceptor: import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; @Injectable() export class fwcAPIInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const