Unverified Commit ec440d4b authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Allow Empty API Body (#9838)

parent 3013c539
......@@ -104,6 +104,11 @@ export async function parseBody(req: NextApiRequest, limit: string | number) {
* @param str `JSON` string
*/
function parseJson(str: string) {
if (str.length === 0) {
// special-case empty json body, as it's a common client-side mistake
return {}
}
try {
return JSON.parse(str)
} catch (e) {
......
......@@ -75,6 +75,17 @@ function runTests(dev = false) {
expect(data).toEqual([{ title: 'Nextjs' }])
})
it('should special-case empty JSON body', async () => {
const data = await fetchViaHTTP(appPort, '/api/parse', null, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
}).then(res => res.ok && res.json())
expect(data).toEqual({})
})
it('should return error with invalid JSON', async () => {
const data = await fetchViaHTTP(appPort, '/api/parse', null, {
method: 'POST',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment