5 lines
17 KiB
JSON

{
"id": "api/common/http/HttpContext",
"title": "HttpContext",
"contents": "\n\n<article>\n <div class=\"breadcrumb-container\">\n <div class=\"breadcrumb\">\n <script type=\"application/ld+json\">\n {\n \"@context\": \"http://schema.org\",\n \"@type\": \"BreadcrumbList\",\n \"itemListElement\": [\n { \"@type\": \"ListItem\", \"position\": 1, \"item\": { \"@id\": \"https://angular.io//api\", \"name\": \"API\" } },\n { \"@type\": \"ListItem\", \"position\": 2, \"item\": { \"@id\": \"https://angular.io/api/common\", \"name\": \"@angular/common\" } },\n { \"@type\": \"ListItem\", \"position\": 3, \"item\": { \"@id\": \"https://angular.io/api/common/http\", \"name\": \"@angular/common/http\" } },\n { \"@type\": \"ListItem\", \"position\": 4, \"item\": { \"@id\": \"https://angular.io/api/common/http/HttpContext\", \"name\": \"HttpContext\" } }\n ]\n }\n </script>\n <a href=\"/api\">API</a> > <a href=\"api/common\">@angular/common</a> > <a href=\"api/common/http\">@angular/common/http</a>\n </div>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L17-L99\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L17-L99\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n \n <header class=\"api-header\">\n <h1 id=\"httpcontext\">HttpContext<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#httpcontext\"><i class=\"material-icons\">link</i></a></h1>\n \n <label class=\"api-type-label class\">class</label>\n \n \n \n </header>\n \n <aio-toc class=\"embedded\"></aio-toc>\n\n <div class=\"api-body\">\n \n <section class=\"short-description\">\n <p>Http context stores arbitrary user defined values and ensures type safety without\nactually knowing the types. It is backed by a <code>Map</code> and guarantees that keys do not clash.</p>\n\n <p><a href=\"api/common/http/HttpContext#description\">See more...</a></p>\n </section>\n \n \n \n <section class=\"class-overview\">\n<code-example language=\"ts\" hidecopy=\"true\">\nclass <a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a> {\n <a class=\"code-anchor\" href=\"api/common/http/HttpContext#set\"><span class=\"member-name\">set</span>&#x3C;T>(token: HttpContextToken&#x3C;T>, value: T): HttpContext</a>\n <a class=\"code-anchor\" href=\"api/common/http/HttpContext#get\"><span class=\"member-name\">get</span>&#x3C;T>(token: HttpContextToken&#x3C;T>): T</a>\n <a class=\"code-anchor\" href=\"api/common/http/HttpContext#delete\"><span class=\"member-name\">delete</span>(token: HttpContextToken&#x3C;unknown>): HttpContext</a>\n <a class=\"code-anchor\" href=\"api/common/http/HttpContext#keys\"><span class=\"member-name\">keys</span>(): IterableIterator&#x3C;HttpContextToken&#x3C;unknown>></a>\n}\n</code-example>\n\n \n \n\n</section>\n\n\n \n\n \n \n<section class=\"description\">\n <h2 id=\"description\">Description<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#description\"><i class=\"material-icons\">link</i></a></h2>\n <p>This context is mutable and is shared between cloned requests unless explicitly specified.</p>\n\n <p>Further information available in the <a href=\"api/common/http/HttpContext#usage-notes\">Usage Notes</a>...</p>\n</section>\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n<section class=\"instance-methods\">\n <h2 id=\"methods\">Methods<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#methods\"><i class=\"material-icons\">link</i></a></h2>\n \n <a id=\"set\"></a>\n<table class=\"is-full-width method-table instance-method\">\n <thead><tr><th>\n <div class=\"with-github-links\">\n <h3 id=\"set\">\n set()\n \n <a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#set\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L53-L65\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L53-L65\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th></tr></thead>\n <tbody>\n <tr>\n <td class=\"short-description\">\n <p>Store a value in the context. If a value is already present it will be overwritten.</p>\n\n </td>\n </tr>\n \n <tr>\n <td>\n <div class=\"overload-info\">\n \n\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\"><span class=\"member-name\">set</span>&#x3C;T>(token: <a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;T>, value: T): <a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a></code-example>\n\n \n\n <h6 class=\"no-anchor\" id=\"parameters\">Parameters</h6>\n <table class=\"is-full-width list-table parameters-table instance-method-overload-parameters\">\n <tbody>\n \n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>token</code>\n </td>\n <td class=\"param-type\"><code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a><t></t></code></td>\n <td class=\"param-description\">\n <p>The reference to an instance of <code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a></code>.</p>\n\n </td>\n </tr>\n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>value</code>\n </td>\n <td class=\"param-type\"><code>T</code></td>\n <td class=\"param-description\">\n <p>The value to store.</p>\n\n </td>\n </tr>\n </tbody>\n</table>\n\n \n <h6 class=\"no-anchor\" id=\"returns\">Returns</h6>\n <p><code><a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a></code>: A reference to itself for easy chaining.</p>\n\n \n\n\n \n\n \n</div>\n </td>\n </tr>\n \n\n \n\n \n </tbody>\n</table>\n\n \n <a id=\"get\"></a>\n<table class=\"is-full-width method-table instance-method\">\n <thead><tr><th>\n <div class=\"with-github-links\">\n <h3 id=\"get\">\n get()\n \n <a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#get\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L66-L79\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L66-L79\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th></tr></thead>\n <tbody>\n <tr>\n <td class=\"short-description\">\n <p>Retrieve the value associated with the given token.</p>\n\n </td>\n </tr>\n \n <tr>\n <td>\n <div class=\"overload-info\">\n \n\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\"><span class=\"member-name\">get</span>&#x3C;T>(token: <a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;T>): T</code-example>\n\n \n\n <h6 class=\"no-anchor\" id=\"parameters-1\">Parameters</h6>\n <table class=\"is-full-width list-table parameters-table instance-method-overload-parameters\">\n <tbody>\n \n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>token</code>\n </td>\n <td class=\"param-type\"><code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a><t></t></code></td>\n <td class=\"param-description\">\n <p>The reference to an instance of <code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a></code>.</p>\n\n </td>\n </tr>\n </tbody>\n</table>\n\n \n <h6 class=\"no-anchor\" id=\"returns-1\">Returns</h6>\n <p><code>T</code>: The stored value or default if one is defined.</p>\n\n \n\n\n \n\n \n</div>\n </td>\n </tr>\n \n\n \n\n \n </tbody>\n</table>\n\n \n <a id=\"delete\"></a>\n<table class=\"is-full-width method-table instance-method\">\n <thead><tr><th>\n <div class=\"with-github-links\">\n <h3 id=\"delete\">\n delete()\n \n <a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#delete\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L80-L91\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L80-L91\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th></tr></thead>\n <tbody>\n <tr>\n <td class=\"short-description\">\n <p>Delete the value associated with the given token.</p>\n\n </td>\n </tr>\n \n <tr>\n <td>\n <div class=\"overload-info\">\n \n\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\"><span class=\"member-name\">delete</span>(token: <a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;unknown>): <a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a></code-example>\n\n \n\n <h6 class=\"no-anchor\" id=\"parameters-2\">Parameters</h6>\n <table class=\"is-full-width list-table parameters-table instance-method-overload-parameters\">\n <tbody>\n \n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>token</code>\n </td>\n <td class=\"param-type\"><code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a><unknown></unknown></code></td>\n <td class=\"param-description\">\n <p>The reference to an instance of <code><a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a></code>.</p>\n\n </td>\n </tr>\n </tbody>\n</table>\n\n \n <h6 class=\"no-anchor\" id=\"returns-2\">Returns</h6>\n <p><code><a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a></code>: A reference to itself for easy chaining.</p>\n\n \n\n\n \n\n \n</div>\n </td>\n </tr>\n \n\n \n\n \n </tbody>\n</table>\n\n \n <a id=\"keys\"></a>\n<table class=\"is-full-width method-table instance-method\">\n <thead><tr><th>\n <div class=\"with-github-links\">\n <h3 id=\"keys\">\n keys()\n \n <a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#keys\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L92-L98\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L92-L98\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th></tr></thead>\n <tbody>\n \n \n <tr>\n <td>\n <div class=\"overload-info\">\n \n\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\"><span class=\"member-name\">keys</span>(): IterableIterator&#x3C;<a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;unknown>></code-example>\n\n \n\n <h6 class=\"no-anchor\" id=\"parameters-3\">Parameters</h6>\n <p>There are no parameters.</p>\n\n \n <h6 class=\"no-anchor\" id=\"returns-3\">Returns</h6>\n <p><code>IterableIterator&#x3C;<a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;unknown>></code>: a list of tokens currently stored in the context.</p>\n\n \n\n\n \n\n \n</div>\n </td>\n </tr>\n \n\n \n\n \n </tbody>\n</table>\n\n \n</section>\n\n\n\n \n<section class=\"usage-notes\">\n <h2 id=\"usage-notes\">Usage notes<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#usage-notes\"><i class=\"material-icons\">link</i></a></h2>\n <h3 id=\"usage-example\">Usage Example<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/http/HttpContext#usage-example\"><i class=\"material-icons\">link</i></a></h3>\n<code-example language=\"typescript\">\n// inside cache.interceptors.ts\nexport const IS_CACHE_ENABLED = new <a href=\"api/common/http/HttpContextToken\" class=\"code-anchor\">HttpContextToken</a>&#x3C;boolean>(() => false);\n\nexport class CacheInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n\n intercept(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a>&#x3C;any>, delegate: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>): Observable&#x3C;<a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a>&#x3C;any>> {\n if (req.context.get(IS_CACHE_ENABLED) === true) {\n return ...;\n }\n return delegate.handle(req);\n }\n}\n\n// inside a service\n\nthis.httpClient.get('/api/weather', {\n context: new <a href=\"api/common/http/HttpContext\" class=\"code-anchor\">HttpContext</a>().set(IS_CACHE_ENABLED, true)\n}).subscribe(...);\n</code-example>\n\n</section>\n\n\n\n </div>\n</article>\n\n<!-- links to this doc:\n - api/common/http\n - api/common/http/HttpClient\n - api/common/http/HttpContextToken\n - api/common/http/HttpRequest\n-->\n<!-- links from this doc:\n - /api\n - api/common\n - api/common/http\n - api/common/http/HttpContext#delete\n - api/common/http/HttpContext#description\n - api/common/http/HttpContext#get\n - api/common/http/HttpContext#httpcontext\n - api/common/http/HttpContext#keys\n - api/common/http/HttpContext#methods\n - api/common/http/HttpContext#set\n - api/common/http/HttpContext#usage-example\n - api/common/http/HttpContext#usage-notes\n - api/common/http/HttpContextToken\n - api/common/http/HttpEvent\n - api/common/http/HttpHandler\n - api/common/http/HttpInterceptor\n - api/common/http/HttpRequest\n - https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L17-L99\n - https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L53-L65\n - https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L66-L79\n - https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L80-L91\n - https://github.com/angular/angular/edit/master/packages/common/http/src/context.ts?message=docs(common)%3A%20describe%20your%20change...#L92-L98\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L17-L99\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L53-L65\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L66-L79\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L80-L91\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/http/src/context.ts#L92-L98\n-->"
}