Skip to content
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

Request.hasBody throws JSON input error when no body has been sent #477

Open
ferkanzai opened this issue Feb 18, 2022 · 3 comments
Open

Comments

@ferkanzai
Copy link

ferkanzai commented Feb 18, 2022

Hi,

I'm having an issue with Deno 1.18.2 and oak 10.2.1 related with #430 where I'm sending a POST request with body set to none using Postman and I get an Internal Server Error with the following message:

[uncaught application error]: SyntaxError - Unexpected end of JSON input

request: { url: "http://localhost:8000/v1/products/", method: "POST", hasBody: true }

Also, in this case as you can see .hasBody is set to true. I know since #430 was resolved, this means the request might have a body, but I'm unable to check the inside of the body value as when trying to log it on the console it throws the aforementioned error. I'm trying this in order to check the content of value to return a 404 response in case is undefined.

Also, when trying this with curl (curl -X POST --url http://localhost:8000/v1/products -H Content-Type:\ application/json) instead of Postman instead of the error above the following gets thrown:

[uncaught application error]: RangeError - Body exceeds a limit of 10485760.

request: { url: "http://localhost:8000/v1/products", method: "POST", hasBody: true }

I'm not 100% sure how to proceed from here, but I think something odd is going on here.

Thanks.

@ferkanzai
Copy link
Author

I've been doing some more research and it seems the error comes from here:

oak/body.ts

Line 241 in 76dca1c

JSON.parse(decoder.decode(await this.#valuePromise()));

As when the body comes empty comes as undefined and trying to run that same thing in a deno environment throws the same error:

> const decoder = new TextDecoder()
undefined
> JSON.parse(decoder.decode(undefined))
Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at <anonymous>:2:6

I'm not so sure on how to tackle the solution myself, but if I came up with something I'll post it here.

@EduM22
Copy link

EduM22 commented Mar 30, 2022

I had a similar issue. I solved it with setting Content-Length header on the request to the body size.
"Content-Length": `${JSON.stringify(body).length}`

@ferkanzai
Copy link
Author

I had a similar issue. I solved it with setting Content-Length header on the request to the body size. "Content-Length": `${JSON.stringify(body).length}`

I'll check this! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants