-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 BUG: api route request body is empty with node adapter SSR #3619
Comments
Just did some more testing and it looks like no type of body is received at all. I did tests for
with Insomnia and none of them ever showed up in I also tried a raw http.createServer(function(req, res) {
ssrHandler(req, res, err => {
if(err) {
res.writeHead(500);
res.end(err.toString());
} else {
res.writeHead(404);
res.end();
}
});
}).listen(8080); but that didn't change anything. |
After looking into it a bit more, I found the reason. https://github.com/withastro/astro/blob/main/packages/astro/src/core/app/node.ts#L8 function createRequestFromNodeRequest(req: IncomingMessage): Request {
let url = `http://${req.headers.host}${req.url}`;
const entries = Object.entries(req.headers as Record<string, any>);
let request = new Request(url, {
method: req.method || 'GET',
headers: new Headers(entries),
});
return request;
} If the request is a node request (http.IncomingMessage), the body is omitted in the conversion to a fetch api request. After looking into this issue even more, the solution seems to be non-trivial and/or I lack the knowledge to tackle this problem. |
Thanks for all the investigation @Scalamando! Sorry you hit this bug, the lack of We're pushing hard to knock out any |
Sounds good, no worries! For anybody else that is facing this problem: Although suboptimal I solved this temporarily by sending the stringified json data in a query string and parsing it in the endpoint: 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);
} |
Sorry about this, I'll take this one on. |
I am having a similar issue, using node adapter SSR with a hybrid output target. I am using an example of Firebase authentication (Astro Firebase). It works perfectly with "npm run dev", hovewer when I run "npm run build" and "node ./dist/server/entry.mjs", I am able to access only plain URL. I tried the workaround provided by Scalamando but the query string disappears too. Any idea what am I doing wrong? CLIENT: SERVER: if(!request.headers.get("authorization")) RESPONSE: {"error":"http://localhost:3000/api/auth/login","author":null,"referer":null,"cookie":null} |
@314ga Did you find a fix for the issue? I'm also seeing this issue. Works with the dev server, but on prod there is no body in the form request. node adapter, server output and can't get the body. I added a middleware and I can see both types of form requests in it, but nothing on the prod.
|
Hey @tayhimself and @rimzzlabs, thanks for reporting! Do you mind seeing if you can reproduce in a minimal example using an astro.new template? That should help us find whether it's a general issue, or something more unique to your setups. |
Hey, thanks for the update, I will be back with a codesandbox this evening |
|
@rimzzlabs Hm, I downloaded this sample you sent and I was unable to replicate. Here is what I did:
This seemed to parse my JSON input correctly. Is it possible you're using |
I didn't read the docs carefully, turns out I can export the |
What version of
astro
are you using?1.0.0-beta.40, 1.0.0-beta.47
Are you using an SSR adapter? If so, which one?
Node (v1.2.0)
What package manager are you using?
npm, yarn classic
What operating system are you using?
WSL Debian / Windows
Describe the Bug
Received POST requests that should contain a json body (and a
Content-Type: application/json
header) have an empty body in SSR mode using the node adapter.Example request:
The api endpoint receives the following:
Run the following in the StackBlitz console:
npm run build
node server.mjs
Just to be sure I checked the response using the
express.json()
middleware and the body is available thereLink to Minimal Reproducible Example
https://stackblitz.com/edit/github-1qng5x?file=src/pages/test.ts
GitHub Repo with MRE
https://github.com/Scalamando/astro-ssr-node-issue-repro
Simply run
yarn && yarn start-ssr
Participation
The text was updated successfully, but these errors were encountered: