HttpContext
Http context stores arbitrary user defined values and ensures type safety without
actually knowing the types. It is backed by a Map and guarantees that keys do not clash.
set
HttpContextStore a value in the context. If a value is already present it will be overwritten.
TThe value to store.
get
TRetrieve the value associated with the given token.
TThe stored value or default if one is defined.
delete
HttpContextDelete the value associated with the given token.
has
booleanChecks for existence of a given token.
booleanTrue if the token exists, false otherwise.
keys
IterableIterator<HttpContextToken<unknown>>IterableIterator<HttpContextToken<unknown>>a list of tokens currently stored in the context.
Description
Http context stores arbitrary user defined values and ensures type safety without
actually knowing the types. It is backed by a Map and guarantees that keys do not clash.
This context is mutable and is shared between cloned requests unless explicitly specified.
Usage Notes
Usage Example
// inside cache.interceptors.ts
export const IS_CACHE_ENABLED = new HttpContextToken<boolean>(() => false);
export class CacheInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, delegate: HttpHandler): Observable<HttpEvent<any>> {
if (req.context.get(IS_CACHE_ENABLED) === true) {
return ...;
}
return delegate.handle(req);
}
}
// inside a service
this.httpClient.get('/api/weather', {
context: new HttpContext().set(IS_CACHE_ENABLED, true)
}).subscribe(...);