1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

fix(cli/web): Support URLSearchParam as Body (#6416)

The following used to fail in Deno despite working in the browser:

```javascript
new Request('http://localhost/', {method: 'POST', body: new URLSearchParams({hello: 'world'})}).text().then(console.log)
```
This commit is contained in:
Chris Couzens 2020-06-24 04:56:05 +01:00 committed by GitHub
parent 49c54c0805
commit f6a4146973
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -25,6 +25,8 @@ function validateBodyType(owner: Body, bodySource: BodyInit | null): boolean {
return true;
} else if (bodySource instanceof FormData) {
return true;
} else if (bodySource instanceof URLSearchParams) {
return true;
} else if (!bodySource) {
return true; // null body is fine
}
@ -193,7 +195,10 @@ export class Body implements domTypes.Body {
);
} else if (this._bodySource instanceof ReadableStreamImpl) {
return bufferFromStream(this._bodySource.getReader());
} else if (this._bodySource instanceof FormData) {
} else if (
this._bodySource instanceof FormData ||
this._bodySource instanceof URLSearchParams
) {
const enc = new TextEncoder();
return Promise.resolve(
enc.encode(this._bodySource.toString()).buffer as ArrayBuffer

View file

@ -72,3 +72,10 @@ unitTest(
assertEquals(formData.get("field_2")!.toString(), "<Deno>");
}
);
unitTest({ perms: {} }, async function bodyURLSearchParams(): Promise<void> {
const body = buildBody(new URLSearchParams({ hello: "world" }));
const text = await body.text();
assertEquals(text, "hello=world");
});