angular-http-interceptors

Angular httpClient interceptor error handling

*爱你&永不变心* 提交于 2019-12-04 06:16:17
after reading the documentation on angular about http client error handling, I still don't understand why I don't catch a 401 error from the server with the code below: export class interceptor implements HttpInterceptor { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.log('this log is printed on the console!'); return next.handle(request).do(() => (err: any) => { console.log('this log isn't'); if (err instanceof HttpErrorResponse) { if (err.status === 401) { console.log('nor this one!'); } } }); } } on the console log, I also get this: zone.js

Angular Interceptor to add Token and Automatically Refresh

与世无争的帅哥 提交于 2019-12-04 05:48:47
问题 I'm working with angular interceptors for the first time and I almost have what I want but there is something I can't quite figure out even after googling around for a while. I am storing a refresh token locally and the access token expires every 15 minutes; I want to be able to use the refresh token to automatically refresh their auth token when it expires. My first attempt went like this: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { if (req.url

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

半腔热情 提交于 2019-12-03 21:09:34
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: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .pipe(

Angular 6 Boilerplate with only Basic Features (Required all the time)?

不想你离开。 提交于 2019-12-03 21:00:56
I Setup Angular all the time with new Project. Anyone can suggest good boilerplate where no need to develop Basic required features which are required most of the times in a application. JWT Authentication in Angular 6 Bootstrap/Material Design setup in Angular 6 Http Services in Angular 6 File Uploading in Angular 6 Basic CRUD in Angular 6 Please suggest. It will save my lot of time. You can follow this boilerplate https://github.com/brnrajoriya/Angular-Ready-To-Use-Boilerplate Just clone this project and run npm install and you got all necessary components and a CRUD with JWT authentication

Angular5 HttpInterceptor depending on the result of an Observable

岁酱吖の 提交于 2019-12-03 20:56:17
问题 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()

Retry failed requests with $http interceptor

走远了吗. 提交于 2019-12-03 17:22:31
问题 The API my webapp is talking to sometimes overloads and is sending 500 Internal Server Error if it cannot handle request. There are 100+ different requests my web application can send, so if I implement retry on each individually, it will cost me hours of typing. I'm already using $httpProvider interceptor, here it is (simplified) $httpProvider.interceptors.push(function ($q) { return { responseError: function (response) { switch (response.status) { case 401 : window.location = "/"; alert(

inject $route into a http interceptor

限于喜欢 提交于 2019-12-03 15:55: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 working code myApp.config(['$httpProvider', function ($httpProvider) { var requestInterceptor = ['$q', '

Accessing HTTP Error Response Body from HttpInterceptor in Angular

岁酱吖の 提交于 2019-12-03 12:13:17
问题 I have an HttpInterceptor to catch errors and display them in a modal. Besides error code and message, I would also like to show the body of the response which actually holds a more precise description of the error (e.g. on a 500 internal server error). How can I achieve this in angular? (I am using version 4.3.6.) I already looked at related questions but answers like HttpErrorResponse._body or similar don't work for me. Also, when inspecting the error response in the console,

Angular 5 Http Interceptor refreshing JWT token

心不动则不痛 提交于 2019-12-03 10:05:07
问题 I already have implemented logic of token saving, retrieving and I have refreshing call also. The problem is that when I intercept 403 in my HttpInterceptor, other calls that are made at the same time, also refresh the token. I would love to hold those calls until my token is refreshed. To create what I would call a 'semaphore' of requests. @Injectable() export class TokenInterceptor implements HttpInterceptor { private auth: AuthService; constructor(private injector: Injector) { } intercept

Angular 6 - Why is Bearer Token missing in production build? (works fine in dev build)

我是研究僧i 提交于 2019-12-03 09:52:09
I'm using Angular 6 with an HTTP Interceptor configured to apply bearer token to outgoing requests. In the dev build ( ng serve ), the token is applied and everything works fine. :-) In the production build ( ng serve --prod ) the request is sent out without bearer token. :-( In the prod build, I have verified the header is being applied, by dumping the headers to the console after applying them. I have no idea why they are excluded from the http request. There are NO differences in my environment files. What else should I be looking at? What can I do to fix this? At first I thought this was