From 377d1a0aea661d09509b189b25e8a9bb95a78705 Mon Sep 17 00:00:00 2001 From: SkyZeroZx <73321943+SkyZeroZx@users.noreply.github.com> Date: Sun, 28 Sep 2025 11:32:56 -0500 Subject: [PATCH] docs: Adds guide for custom HTTP parameter encoding (#64119) PR Close #64119 --- .../src/content/guide/http/making-requests.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/adev/src/content/guide/http/making-requests.md b/adev/src/content/guide/http/making-requests.md index 96569efde02..180f9f3bdba 100644 --- a/adev/src/content/guide/http/making-requests.md +++ b/adev/src/content/guide/http/making-requests.md @@ -104,6 +104,49 @@ http.get('/api/config', { You can instantiate `HttpParams` with a custom `HttpParameterCodec` that determines how `HttpClient` will encode the parameters into the URL. +### Custom parameter encoding + +By default, `HttpParams` uses the built-in [`HttpUrlEncodingCodec`](api/common/http/HttpUrlEncodingCodec) to encode and decode parameter keys and values. + +You can provide your own implementation of [`HttpParameterCodec`](api/common/http/HttpParameterCodec) to customize how encoding and decoding are applied. + +```ts +import { HttpClient, HttpParams, HttpParameterCodec } from '@angular/common/http'; +import { inject } from '@angular/core'; + +export class CustomHttpParamEncoder implements HttpParameterCodec { + encodeKey(key: string): string { + return encodeURIComponent(key); + } + + encodeValue(value: string): string { + return encodeURIComponent(value); + } + + decodeKey(key: string): string { + return decodeURIComponent(key); + } + + decodeValue(value: string): string { + return decodeURIComponent(value); + } +} + +export class ApiService { + private http = inject(HttpClient); + + search() { + const params = new HttpParams({ + encoder: new CustomHttpParamEncoder(), + }) + .set('email', 'dev+alerts@example.com') + .set('q', 'a & b? c/d = e'); + + return this.http.get('/api/items', { params }); + } +} +``` + ## Setting request headers Specify request headers that should be included in the request using the `headers` option.