Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-terms-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly return 415 when unexpected content types are submitted to actions
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/page/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ async function call_action(event, actions) {
}

if (!is_form_content_type(event.request)) {
throw new Error(
throw error(
415,
`Actions expect form-encoded data (received ${event.request.headers.get('content-type')})`
);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,21 @@ test.describe('Actions', () => {

await expect(page.locator('pre')).toHaveText('something went wrong');
});

test('submitting application/json should return http status code 415', async ({ baseURL }) => {
const response = await fetch(`${baseURL}/actions/form-errors`, {
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
headers: {
'Content-Type': 'application/json',
Origin: `${baseURL}`
}
});
const { type, error } = await response.json();
expect(type).toBe('error');
expect(error.message).toBe('Actions expect form-encoded data (received application/json)');
expect(response.status).toBe(415);
});
});

// Run in serial to not pollute the log with (correct) cookie warnings
Expand Down