supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

Body can not be parsed in edge functions

Open BenBao93 opened this issue 4 years ago • 2 comments

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.

BenBao93 avatar Nov 09 '22 14:11 BenBao93

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 avatar Nov 11 '22 23:11 GaryAustin1

@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 })
  }

BenBao93 avatar Nov 13 '22 17:11 BenBao93