Skip to content

Commit 4bc3cf2

Browse files
ajitsinghkaleralxhub
authored andcommitted
feat(common): add URLSearchParams to request body (angular#37852)
URLSearch params are by default supported in the browser but are not supported by angular/http package added support for URLSearchParams Fixes angular#36317 PR Close angular#37852
1 parent e1c5cea commit 4bc3cf2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/common/http/src/request.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ function isFormData(value: any): value is FormData {
6868
return typeof FormData !== 'undefined' && value instanceof FormData;
6969
}
7070

71+
/**
72+
* Safely assert whether the given value is a URLSearchParams instance.
73+
*
74+
* In some execution environments URLSearchParams is not defined.
75+
*/
76+
function isUrlSearchParams(value: any): value is URLSearchParams {
77+
return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams;
78+
}
79+
7180
/**
7281
* An outgoing HTTP request with an optional typed body.
7382
*
@@ -273,7 +282,7 @@ export class HttpRequest<T> {
273282
// Check whether the body is already in a serialized form. If so,
274283
// it can just be returned directly.
275284
if (isArrayBuffer(this.body) || isBlob(this.body) || isFormData(this.body) ||
276-
typeof this.body === 'string') {
285+
isUrlSearchParams(this.body) || typeof this.body === 'string') {
277286
return this.body;
278287
}
279288
// Check whether the body is an instance of HttpUrlEncodedParams.

packages/common/http/test/request_spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ const TEST_STRING = `I'm a body!`;
143143
const body = new ArrayBuffer(4);
144144
expect(baseReq.clone({body}).serializeBody()).toBe(body);
145145
});
146+
it('passes URLSearchParams through', () => {
147+
const body = new URLSearchParams('foo=1&bar=2');
148+
expect(baseReq.clone({body}).serializeBody()).toBe(body);
149+
});
146150
it('passes strings through', () => {
147151
const body = 'hello world';
148152
expect(baseReq.clone({body}).serializeBody()).toBe(body);

0 commit comments

Comments
 (0)