angular-http-interceptors

Angular Interceptor route to a different path based on response

烈酒焚心 提交于 2019-12-06 23:47:35
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: LocalStorage = this._localStorageService.getItem('auth-token'); if (authToken) { return next.handle( req.clone({

Angular 4 - Callback for Observable.do() does not get called in interceptor

安稳与你 提交于 2019-12-06 16:00:20
问题 Trying to implement solution for displaying a loading indicator when requests are in progress. Looking from this solution I implemented the interceptor with the service. All works fine, except the counter is not deceasing becase the .do() callback never gets executed ( b is never printed in the console). Any thoughts on that? How to know if request has finished? import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common

Overload request headers and params with HttpClient get

大憨熊 提交于 2019-12-06 06:29:32
In HttpClientModule, is there a method to pass headers and params to get request. import { HttpHeaders, HttpParams, HttpClient } from @angular/common/http'; const headers = { headers: new HttpHeaders({}) } let params = new HttpParams({ }); get(url, {params}) // http client get with params get(url, {headers}); //http client get with headers I want something like requestoptions to hold both or a syntax to do httpClient get sending request headers and params. Currently building complete url with search params and sending headers along. Here is something that passes both headers and parameters in

Shared AngularJS $http interceptors

一个人想着一个人 提交于 2019-12-05 21:03:35
I am wondering if Angular.js $http interceptors are shared thoughout the whole application. Let's assume I have a myDependentApp module, shared between many apps. That module has some interceptor configured to control the $http requests/responses. I include that module by declaring it in application bootstrap: angular.module('myApp', ['myDependentApp']); And I have application template: <html ng-app="myApp"> Are myDependentApp 's interceptors going to be active in myApp ? Thanks for help. The answer is yes, I tried it here: var dependentApp = angular.module('dependency',[]).config(['

what is the difference between error handler and interceptor in angular 2?

偶尔善良 提交于 2019-12-05 18:20:42
And also what would be the best solution for front end error handling in ng2 for real-time web app? Is it fine to use 'Http interceptor' for front end error handling? Please explain about these things. What is the difference between error handler and interceptor in angular 2?: Based on the Angular documentation, this is how they are defined: ErrorHandler : The default implementation of ErrorHandler prints error messages to the console. HttpInterceptor : Typically an interceptor will transform the outgoing request before returning next.handle(transformedReq). An interceptor may choose to

Angular HttpInterceptor not changing header

冷暖自知 提交于 2019-12-05 16:36:19
I wrote an angular (4.3.6) HttpInterceptor to add some header fields, but the header doesn't get updated if I inspect them in the debugger. Any idea? import {Injectable} from '@angular/core'; import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; @Injectable() export class AuthInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.log('AuthInterceptor at work'); const contentTypeReq = req.clone({ headers: req.headers.set('Content-Type',

Request Header is not updated successfully from interceptor angular 2/4 (401 handling)

旧巷老猫 提交于 2019-12-05 10:49:05
I am working with Http interceptor and trying to retry the failed request to handle 401 error . I am trying to set a new header to update the request but it's not working. I noticed that My header is not being set with the request instead it's going to the lazyUpdates inside headers. Can anyone provide me the any idea why it's happening. After checking my networks I found that with the retry request old header is passed which is 'x-auth-token' and new headers are not sent. interceptor.ts intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { request = request

Angular - HTTP interceptor to retry requests with specific error status?

人盡茶涼 提交于 2019-12-05 05:09:44
问题 I am trying to use an interceptor to handle http errors and retry for a special error status, in my case the status code 502. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .pipe( retryWhen(errors => { return errors .pipe( mergeMap(error => (error.status === 502) ? throwError(error) : of(error)), take(2) ) }) ) } But it's not working, whereas when I am using retry() in this fashion, it's working perfectly intercept(request:

inject $route into a http interceptor

情到浓时终转凉″ 提交于 2019-12-05 00:16:59
问题 I am trying to inject custom header to every API request. When I am providing some hard coded text it works. Working code myApp.config(['$httpProvider', function ($httpProvider) { var requestInterceptor = ['$q', '$rootScope', function ($q, $rootScope) { var interceptorInstance = { request: function (config) { config.headers['X-MyApp-CustomHeader'] = "foobar"; return config || $q.when(config); } }; return interceptorInstance; }]; $httpProvider.interceptors.push(requestInterceptor); }]); Not

Angular 4 - Callback for Observable.do() does not get called in interceptor

前提是你 提交于 2019-12-04 21:30:44
Trying to implement solution for displaying a loading indicator when requests are in progress. Looking from this solution I implemented the interceptor with the service. All works fine, except the counter is not deceasing becase the .do() callback never gets executed ( b is never printed in the console). Any thoughts on that? How to know if request has finished? import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/do'; import {