diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index 94c232a47bca..0b8baa660535 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -63,7 +63,7 @@ export default async function render_route(request, route) { (!type || type.startsWith('application/json')) ) { headers = { ...headers, 'content-type': 'application/json; charset=utf-8' }; - normalized_body = JSON.stringify(body || {}); + normalized_body = JSON.stringify(typeof body === 'undefined' ? {} : body); } else { normalized_body = /** @type {import('types/hooks').StrictBody} */ (body); } diff --git a/packages/kit/test/apps/basics/src/routes/endpoint-output/_tests.js b/packages/kit/test/apps/basics/src/routes/endpoint-output/_tests.js index a65e96fe43b8..781e73461fbe 100644 --- a/packages/kit/test/apps/basics/src/routes/endpoint-output/_tests.js +++ b/packages/kit/test/apps/basics/src/routes/endpoint-output/_tests.js @@ -36,4 +36,10 @@ export default function (test) { const res = await fetch('/endpoint-output/empty'); assert.type(await res.buffer(), 'object'); }); + + test('null body returns null json value', null, async ({ fetch }) => { + const res = await fetch('/endpoint-output/null'); + assert.equal(res.status, 200); + assert.equal(await res.json(), null); + }); } diff --git a/packages/kit/test/apps/basics/src/routes/endpoint-output/null.js b/packages/kit/test/apps/basics/src/routes/endpoint-output/null.js new file mode 100644 index 000000000000..d6d2b9f71fae --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/endpoint-output/null.js @@ -0,0 +1,4 @@ +/** @type {import('@sveltejs/kit').RequestHandler} */ +export function get() { + return { body: null }; +}