Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): add URLSearchParams to request body #37852

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/common/http/src/request.ts
Expand Up @@ -68,6 +68,15 @@ function isFormData(value: any): value is FormData {
return typeof FormData !== 'undefined' && value instanceof FormData;
}

/**
* Safely assert whether the given value is a URLSearchParams instance.
*
* In some execution environments URLSearchParams is not defined.
*/
function isUrlSearchParams(value: any): value is URLSearchParams {
petebacondarwin marked this conversation as resolved.
Show resolved Hide resolved
return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams;
ajitsinghkaler marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* An outgoing HTTP request with an optional typed body.
*
Expand Down Expand Up @@ -273,7 +282,7 @@ export class HttpRequest<T> {
// Check whether the body is already in a serialized form. If so,
// it can just be returned directly.
if (isArrayBuffer(this.body) || isBlob(this.body) || isFormData(this.body) ||
typeof this.body === 'string') {
isUrlSearchParams(this.body) || typeof this.body === 'string') {
return this.body;
}
// Check whether the body is an instance of HttpUrlEncodedParams.
Expand Down
4 changes: 4 additions & 0 deletions packages/common/http/test/request_spec.ts
Expand Up @@ -143,6 +143,10 @@ const TEST_STRING = `I'm a body!`;
const body = new ArrayBuffer(4);
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('passes URLSearchParams through', () => {
const body = new URLSearchParams('foo=1&bar=2');
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('passes strings through', () => {
const body = 'hello world';
expect(baseReq.clone({body}).serializeBody()).toBe(body);
Expand Down