0

I am having trouble with vercel serverless functions, in that I am getting the following error:

artwork route hit!

error SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at parseJSONFromBytes (node:internal/deps/undici/undici:6571:19)
at successSteps (node:internal/deps/undici/undici:6545:27)
at node:internal/deps/undici/undici:1211:60
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

This error occurs at an attempt to call await req.json() within a route handler of a next project (app router, /api folder).

These routes work fine on my local machine, and the request is being sent in the correct format. I have this app deployed as a long running service on another platform, and there are no issues when I route requests there either - This leads me to believe that there is an issue with my vercel setup.

// imports..
export async function POST(req: NextRequest) {
    console.log("artwork route hit!"); // this is logged
    
    if (!req.body) {
        console.log("no body found") // this is not logged
        return new NextResponse("No request body found", { status: 401 }); 
    }

    const job: Job = await req.json();
    console.log(`new job parsed: ${job.id}`) // this is not logged

As mentioned, this error only occurs in the vercel function. locally, and deployed elsewhere, the code runs as expected (body is found, function can parse it and move on). As such, I am having difficulty diagnosing why specifically this runtime is having trouble with req.json().

I can note as well that the middleware from the Next project fires before these requests successfully. I wonder if they may be stripping the body somehow? Again though I am finding this hard to diagnose as the setup is identical to node in localhost and hosted online with no trouble.

Thanks in advance for your help.

1
  • Can you share the CURL of how you call the vercel POST api? Commented Aug 19, 2023 at 23:11

1 Answer 1

0

I'm experiencing the same issue; with slightly different Next-14 APP router handler setup. The issue is the same.

JSON sometime missing last "}" without any reason. The tests pass in local environment no problem.

{"type":"content_block_delta","index":0,"delta {"type":"text_delta","text":"Hello"

I used a recursive while function that adds } to the end of the string when the number of } at the end is below a certain number. This could be a result of API handling of the NEXT.JS system.

Hope it help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.