#OpenNext worker apis not working properly in deployed environment

7 messages · Page 1 of 1 (latest)

sly turret
#

Hello, I have an OpenNext worker, with an api route /api/subscribe. Here is the endpoint:


import { NextResponse } from "next/server"
import { getCloudflareContext } from "@opennextjs/cloudflare";

export async function POST(req: Request) {
  try {
    console.log("POST /api/subscribe");
    console.log("req", req);


    return NextResponse.json({ success: true })
  } catch (error) {
    console.error("Subscription error:", error)
    return NextResponse.json({ error: "Failed to process subscription" }, { status: 500 })
  }
}

The problem is on local environment everything is fine and the logs are :

POST /api/subscribe
req Request {
  method: 'POST',
  url: 'http://localhost:3000/api/subscribe',
  headers: Headers {
    host: 'localhost:3000',
    connection: 'keep-alive',
    'content-length': '25',
    'sec-ch-ua-platform': '"Windows"',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
    'content-type': 'application/json',
    'sec-ch-ua-mobile': '?0',
    accept: '*/*',
    origin: 'http://localhost:3000',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    referer: 'http://localhost:3000/',
    ......
  },
  destination: '',
  referrer: 'about:client',
  referrerPolicy: '',
  mode: 'cors',
  credentials: 'same-origin',
  cache: 'default',
  redirect: 'follow',
  integrity: '',
  keepalive: false,
  isReloadNavigation: false,
  isHistoryNavigation: false,
  signal: AbortSignal { aborted: false }
}

but the deployed version receives empty request:

POST https://caster-fun.kaspiummage.workers.dev/api/subscribe - Ok @ 3/11/2025, 12:48:57 AM
  (log) POST /api/subscribe
  (log) req {}

you can see the structure in the picture.
I had another api with the same problem, am I doing something wrong?

violet pelican
#

You can’t log a Request class

#

Or like most classes in general

sly turret
# violet pelican You can’t log a Request class

ok didn't know that thanks. I was trying to log it because I was trying to extract the body from the request, which is what I need to process. The problem is that when I tried to do so with

    const body = await req.json();

an error occurs

 (error) Subscription error: SyntaxError: "[object Object]" is not valid JSON

which to me it seems like the body is empty. Is this approach of extracting it wrong?

#

this api is supposed to be triggered by a button on click from my page component, this is the call

const response = await fetch("/api/subscribe", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ email }),
      })
#

I'm not very proficient with js/ts but this is what I found online suggested to do it. I was also even more confused because on local environment it was working as expected

sly turret
#

I also just checked the network tab on the deployed version and the request call looks ok