Body can not be parsed in edge functions
Bug report
Describe the bug
When a POST request is made with the JS SDK to an edge functions via the invoke method with a JSON body it causes a server error.
SyntaxError: Unexpected end of JSON input
at parse (<anonymous>)
at packageData (deno:ext/fetch/22_body.js:359:16)
at consumeBody (deno:ext/fetch/22_body.js:236:14)
at async Server.<anonymous> (file:///src/main.ts:6551:26)
at async Server.#respond (file:///src/main.ts:111:24)
The same request with the same body via Insomnia/Postman works without issue.
To Reproduce
Happens with every function as long as the body is parsed and a body is present in the request.
Additional context
I presume this is related to missing headers but it shouldn't happen since the SDK would be setting all required headers for the invoke function.
Could this be caused by the OPTIONS request not being handled in your function? Browser code is going to force CORS preflight requests. Otherwise you don't show an example function or your call, so hard to give more feedback.
@GaryAustin1 it looks like I set the headers for the response but missed the OPTIONS handling at the start of the function as mentioned in this guide.
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}