Note: I havn't found a way to extend the timeout, but this will allow you to edit the timeout to shorter than the default of 2 minutes.
If you are posting something to the backend that takes over 2 minutes to process you should alert the client that you have the data and are trying to process it instead of attempting to extend the timeout.
Here is how you would go about shortening a timeout:
Inside of your app module you should create the interceptor with a custom timeout in milliseconds.
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TimeoutError } from 'rxjs/util/TimeoutError';
import 'rxjs/add/operator/timeout';
import { InjectionToken, Injectable, Inject } from '@angular/core';
import { HttpHandler, HttpRequest, HttpInterceptor, HttpEvent } from '@angular/common/http';
const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
const defaultTimeout = 5000;
@Injectable()
export class TimeoutInterceptor implements HttpInterceptor {
constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const timeout = Number(req.headers.get('timeout')) || this.defaultTimeout;
return next.handle(req).timeout(timeout);
}
}
Add this to your providers list:
[{ provide: HTTP_INTERCEPTORS, useClass: TimeoutInterceptor, multi: true }],
[{ provide: DEFAULT_TIMEOUT, useValue: defaultTimeout }]
When you make the request you want to add a custom header that uses your interceptor, it looks like this:
const options = {
headers: new HttpHeaders({ timeout: `${3600000}` }),
params: params,
reportProgress: true,
Timeout: 5000
};
Now when you make a request that hits the 5000 millisecond timeout it should stop.