diff --git a/chat/index.js b/chat/index.js index f05aab0..1922c9d 100644 --- a/chat/index.js +++ b/chat/index.js @@ -18,7 +18,18 @@ const handler = async (request) => { } try { - const body = await request.json() + // Use text() and parse manually - json() may not be available in wws + const bodyText = await request.text() + let body + try { + body = JSON.parse(bodyText) + } catch (e) { + let response = new Response(JSON.stringify({ error: 'Invalid JSON body' })) + response.headers.set('Content-Type', 'application/json') + response.headers.set('Access-Control-Allow-Origin', '*') + return response + } + const message = body.message const project_id = body.project_id const user_id = body.user_id @@ -45,7 +56,7 @@ const handler = async (request) => { }) }) - // Use text() and parse manually (json() may not be available in wws) + // Use text() and parse manually const responseText = await n8nResponse.text() let result try {