From 2bbc43505e48b89acd802d6c09d8bb03d5561bfc Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 28 Jun 2026 13:22:01 -0500 Subject: [PATCH] feat: parse application/x-www-form-urlencoded request bodies Signed-off-by: Diego Resendez --- src/http.ts | 2 ++ test/http.test.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/http.ts b/src/http.ts index c4602c0..f08a63e 100644 --- a/src/http.ts +++ b/src/http.ts @@ -207,6 +207,8 @@ export function createHttpServer( res.end(JSON.stringify({ error: 'Invalid JSON body' })); return; } + } else if (raw !== undefined && ct.includes('application/x-www-form-urlencoded')) { + body = Object.fromEntries(new URLSearchParams(raw)); } let result: PipelineResult; try { diff --git a/test/http.test.ts b/test/http.test.ts index f94b407..468592e 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -137,4 +137,18 @@ describe('request hardening', () => { expect(text).toContain('data: {"n":3}'); server.close(); }); + + it('parses application/x-www-form-urlencoded into an object body', async () => { + const getUrl = (s: import('http').Server) => `http://127.0.0.1:${(s.address() as any).port}`; + const server = createHttpServer([{ + method: 'POST', pattern: '/form', transport: 'buffer', + handler: async (req) => ({ status: 200, headers: { 'content-type': 'application/json' }, body: JSON.stringify(req.body) }), + }]); + await new Promise((r) => server.listen(0, r)); + const res = await fetch(`${getUrl(server)}/form`, { + method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'a=1&b=two', + }); + expect(await res.json()).toEqual({ a: '1', b: 'two' }); + server.close(); + }); });