W3cubDocs

/Angular

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.

API

class HttpContext {
  set<T>(token: HttpContextToken<T>, value: T): HttpContext;
  get<T>(token: HttpContextToken<T>): T;
  delete(token: HttpContextToken<unknown>): HttpContext;
  has(token: HttpContextToken<unknown>): boolean;
  keys(): IterableIterator<HttpContextToken<unknown>>;
}

set

HttpContext

Store a value in the context. If a value is already present it will be overwritten.

@paramtokenHttpContextToken<T>

The reference to an instance of HttpContextToken.

@paramvalueT

The value to store.

@returnsHttpContext

get

T

Retrieve the value associated with the given token.

@paramtokenHttpContextToken<T>

The reference to an instance of HttpContextToken.

@returnsT

delete

HttpContext

Delete the value associated with the given token.

@paramtokenHttpContextToken<unknown>

The reference to an instance of HttpContextToken.

@returnsHttpContext

has

boolean

Checks for existence of a given token.

@paramtokenHttpContextToken<unknown>

The reference to an instance of HttpContextToken.

@returnsboolean

keys

IterableIterator<HttpContextToken<unknown>>
@returnsIterableIterator<HttpContextToken<unknown>>

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(...);

Super-powered by Google ©2010–2025.
Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0.
https://angular.dev/api/common/http/HttpContext