angular-http-interceptors

How to mock Angular 4.3 httpClient an error response in testing

ⅰ亾dé卋堺 提交于 2019-12-03 08:27:59
问题 I have a below interceptor auth-interceptor.service.ts import {Injectable, Injector} from '@angular/core'; import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; import {Cookie} from './cookie.service'; import {Router} from '@angular/router'; import {UserService} from './user.service'; import {ToasterService} from '../toaster/toaster.service'; import 'rxjs/add/operator/catch'; import 'rxjs/add

Retry failed requests with $http interceptor

喜欢而已 提交于 2019-12-03 05:46:12
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('Session has expired. Redirecting to login page'); break; case 500 : // TODO: retry the request break; }

Accessing HTTP Error Response Body from HttpInterceptor in Angular

≯℡__Kan透↙ 提交于 2019-12-03 03:32:01
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, HttpErrorResponse.error is set to null. Here is how my interceptor currently looks: @Injectable() export class

How to mock Angular 4.3 httpClient an error response in testing

空扰寡人 提交于 2019-12-03 00:01:43
I have a below interceptor auth-interceptor.service.ts import {Injectable, Injector} from '@angular/core'; import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; import {Cookie} from './cookie.service'; import {Router} from '@angular/router'; import {UserService} from './user.service'; import {ToasterService} from '../toaster/toaster.service'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor

How read the URL and parameter from HTTP interceptor $httpProvider in angular js

那年仲夏 提交于 2019-12-02 21:59:41
问题 where i can get the URL string and the parameter of a POST request from the HttpIntercepter and push that information to IndexedDB. But i have no idea how to get the URL and Parameter from the httpintercepter. angular.module("app").config(['$httpProvider', function ($httpProvider) { if (!$httpProvider.defaults.headers.get) { debugger; $httpProvider.defaults.headers.get = {}; console.log('POST method triggered'); } else { } $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul

Angular 6 Http Interceptors, request headers not modified

本小妞迷上赌 提交于 2019-12-02 19:47:48
问题 I created an interceptor to add an authorization header to each request sent by the client, here is the code : import { HttpInterceptor, HttpRequest, HttpHandler, HttpHeaderResponse, HttpSentEvent, HttpProgressEvent, HttpResponse, HttpUserEvent, HttpEvent, HttpHeaders } from "@angular/common/http"; import { Observable } from "rxjs"; import { Injectable } from "@angular/core"; @Injectable() export class AuthenticationInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>,

Angular Interceptor to add Token and Automatically Refresh

血红的双手。 提交于 2019-12-02 11:14:29
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.toLowerCase().includes('/auth')) { // It's an auth request, don't get a token return next.handle(req); } // Not

How read the URL and parameter from HTTP interceptor $httpProvider in angular js

孤者浪人 提交于 2019-12-02 10:33:28
where i can get the URL string and the parameter of a POST request from the HttpIntercepter and push that information to IndexedDB. But i have no idea how to get the URL and Parameter from the httpintercepter. angular.module("app").config(['$httpProvider', function ($httpProvider) { if (!$httpProvider.defaults.headers.get) { debugger; $httpProvider.defaults.headers.get = {}; console.log('POST method triggered'); } else { } $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; $httpProvider

Angular 6 Http Interceptors, request headers not modified

霸气de小男生 提交于 2019-12-02 09:48:27
I created an interceptor to add an authorization header to each request sent by the client, here is the code : import { HttpInterceptor, HttpRequest, HttpHandler, HttpHeaderResponse, HttpSentEvent, HttpProgressEvent, HttpResponse, HttpUserEvent, HttpEvent, HttpHeaders } from "@angular/common/http"; import { Observable } from "rxjs"; import { Injectable } from "@angular/core"; @Injectable() export class AuthenticationInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.log(localStorage.getItem('jwtToken')); if

handle cancelled http request in angular httpclient interceptor

萝らか妹 提交于 2019-12-02 04:09:35
export class AppHttpInterceptor implements HttpInterceptor { private cache = new HttpCache(); private cacheURLList = []; count = 0; constructor(@Inject(AppBlockUiService) private appBlockUiService: AppBlockUiService, @Inject(AppMessageService) private appMessageService: AppMessageService) { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const started = Date.now(); this.blockUi(); return next.handle(serverReq) .timeout(720000) .do( event => { if (event instanceof HttpResponse) { this.unBlockUi(); } }, err => { if (err instanceof HttpErrorResponse) { // this