oak icon indicating copy to clipboard operation
oak copied to clipboard

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

Open ferkanzai opened this issue 3 years ago • 3 comments

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 avatar Feb 18 '22 02:02 ferkanzai

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

https://github.com/oakserver/oak/blob/76dca1cb794ebffde216b4f33c4b632aca039002/body.ts#L241

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.

ferkanzai avatar Feb 19 '22 05:02 ferkanzai

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}`

EduM22 avatar Mar 30 '22 17:03 EduM22

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!

ferkanzai avatar Mar 30 '22 17:03 ferkanzai