#Astro API endpoint body is empty

7 messages · Page 1 of 1 (latest)

foggy mortar
#
export const POST: APIRoute = async ({ request }) => {
  console.log(await request.body()) // null
}
red gardenBOT
#

Hi @foggy mortar. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the knowledge sources provided, it seems like you're encountering a known issue where the request body is empty when using the Node adapter in SSR mode with Astro. This issue was reported on Astro's GitHub page here (https://github.com/withastro/astro/issues/3619).

The problem arises when the request is a Node request (http.IncomingMessage), the body is omitted in the conversion to a fetch API request. This issue seems to be non-trivial to solve.

As a temporary workaround, one user suggested sending the stringified JSON data in a query string and parsing it in the endpoint. Here's an example:

Client

await fetch(
    `/register?data=${encodeURIComponent(JSON.stringify(dataObj))}`,
    { method: "post" }
);

API Endpoint

export const post: APIRoute = async ({ request }) => {
    const { data } = Object.fromEntries(new URL(request.url).searchParams);
}

Please note that this is a temporary solution and might not be optimal for all use cases. The Astro team is aware of this issue and is working on a fix.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@foggy mortar kapa.ai is still learning and improving, please let us know how it did by reacting below

foggy mortar
#

@red garden That issue has been fixed on github. Any other leads?

red gardenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry, but based on the provided knowledge sources, there's no additional information or updates regarding the issue of Astro API endpoint body being empty. The sources do not contain any other leads or solutions to this problem.

If the issue has been fixed on GitHub, I recommend checking the relevant pull request or discussion for any additional details or potential workarounds that were suggested during the resolution of the issue.

For more detailed assistance, consider reaching out to the Astro community or the maintainers directly on GitHub or their official communication channels. They might be able to provide more context or solutions specific to your situation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: