ory / integrations

Apache License 2.0
19 stars 24 forks source link

Next-edge | ERROR: Unable to decode HTTP Request Body because its HTTP Header \"Content-Length\" is zero. #29

Closed thesocialdev closed 2 years ago

thesocialdev commented 2 years ago

I am having an error when trying to submit a login request to path/to/ory/self-service/login?flow=

The error returned is a 400 Bad Request

Unable to decode HTTP Request Body because its HTTP Header \"Content-Length\" is zero.
Screen Shot 2022-04-05 at 6 25 44 PM

I tried to manually forward the content-length from the next-edge/createApiHandler but then I got ECONNRESET errors

How can I proceed?

thesocialdev commented 2 years ago

@aeneasr

aeneasr commented 2 years ago

Sorry to hear you are having trouble. What repository are you working with? Is there maybe a reproducible repo we can take a look at? I’m on vacation currently but maybe @Benehiko can help out here!

thesocialdev commented 2 years ago

Thanks for the prompt response @aeneasr (please do not disrupt your vacation)

The code to reproduce it is in this PR https://github.com/AletheiaFact/aletheia/pull/342

I'm happy to walk you through setup and tests. But assuming you got into the setup through the readme, setting up the ory sdk credentials and accessing http://localhost:3000/ory-login should work

thesocialdev commented 2 years ago

For context, the only difference from the examples is that I'm wrapping NextJS with NestJS, but still using NextJS Server to perform SSR.

aeneasr commented 2 years ago

I see, in that case the error probably happens because NestJS is dealing with the config differently and is parsing the HTTP body instead of passing it as a raw byte stream down to our systems. In NextJS we disable the body parser using a config value, not sure how that works in NestJS

thesocialdev commented 2 years ago

@aeneasr that's spot on 🎯 the problem was the body parser

aeneasr commented 2 years ago

How did you fix it for NestJS? Might be helpful to others :)

thesocialdev commented 2 years ago

@aeneasr the solution was to disable body parser for the application, you can see that in this line of code. If we need the body parser functionality we need to explicitly filter the endpoints through a middleware.

But now, ory is successfully integrated with our application

thesocialdev commented 2 years ago

And for context, that's how I am enabling body parsing for endpoints not related to Ory integration: https://github.com/AletheiaFact/aletheia/pull/342/commits/de0812535e5187fa78796c038dfbdf11a9f9af43

aeneasr commented 2 years ago

Awesome thank you! @Benehiko could you organize that this makes it into our documentation so others working with NestJS don’t face the same difficulties? Thank you!

Benehiko commented 2 years ago

Will do :)

jetaggart commented 2 months ago

I'm getting this error with nextjs 14.2.4, not using nestjs. Anyone else?

jetaggart commented 2 months ago

Interesting, I downgraded from @ory/integrations 1.2.1 to 1.1.5 and it seems to work fine again. Any idea what may have happened between 1.1.5 and 1.2.1 to cause this with nextjs 14.2.4?

teawithfruit commented 2 months ago

I have the same problem. It looks to me like the fetch implementation is not working properly. https://github.com/ory/integrations/blob/781878c3ecd7119c6a71bfd32d919b3f283d1a44/src/next-edge/index.ts#L110-L118

david-on-github commented 2 months ago

@jetaggart @teawithfruit Removing the imported config that disables body parser fixes it after the 1.2.0 update. Looks like the handler now expects the body to come parsed?

jetaggart commented 2 months ago

@david-on-github do you mind sharing some code or more explanation how you disabled the body parser? Not quite following.

david-on-github commented 2 months ago

@jetaggart

The config from the readme is shown being re-exported. https://github.com/ory/integrations?tab=readme-ov-file#nextjs

import { config, createApiHandler } from "@ory/integrations/next-edge"

export { config }

The config disables bodyparser for nextjs api routes. Just need to remove the export { config } line since the function is treating the body as json.