Skip to content

Commit

Permalink
fix(got): posting form as x-www-form-urlencoded (#15348)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL committed Apr 23, 2024
1 parent 26dcea0 commit 41c6861
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/setup.test.ts
Expand Up @@ -228,6 +228,7 @@ Unknown paragraph
const formData = await request.formData();
return HttpResponse.json({
test: formData.get('test'),
req: { headers: Object.fromEntries(request.headers.entries()) },
});
}),
http.post(`http://rsshub.test/json-post`, async ({ request }) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/got.test.ts
Expand Up @@ -36,8 +36,9 @@ describe('got', () => {
test: 'rsshub',
},
});
expect(response.body).toBe('{"test":"rsshub"}');
expect(response.body).toContain('"test":"rsshub"');
expect(response.data.test).toBe('rsshub');
expect(response.data.req.headers['content-type']).toBe('application/x-www-form-urlencoded');
});

it('json-post', async () => {
Expand Down
7 changes: 2 additions & 5 deletions lib/utils/got.ts
Expand Up @@ -27,14 +27,11 @@ const getFakeGot = (defaultOptions?: any) => {
delete options.json;
}
if (options?.form && !options.body) {
const body = new FormData();
for (const key in options.form) {
body.append(key, options.form[key]);
}
options.body = body;
options.body = new URLSearchParams(options.form as Record<string, string>).toString();
if (!options.headers) {
options.headers = {};
}
options.headers['content-type'] = 'application/x-www-form-urlencoded';
delete options.form;
}
if (options?.searchParams) {
Expand Down

0 comments on commit 41c6861

Please sign in to comment.