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

Throws error on :accept :json when response body is empty. #24

Open
knubie opened this issue Feb 23, 2023 · 2 comments
Open

Throws error on :accept :json when response body is empty. #24

knubie opened this issue Feb 23, 2023 · 2 comments

Comments

@knubie
Copy link

knubie commented Feb 23, 2023

When you specify an :accept :json header, lambdaisland/fetch will attempt to automatically decode the response body using response.json(). However this will fail and raise an error if the response body is empty (ie JSON.parse('')).

(defmethod decode-body :json [_ response opts]
(j/call response :json))

The code should probably check if the body is empty first.

@simongray
Copy link
Contributor

This is sadly expected behaviour when using JS Fetch: whatwg/fetch#113

Why WHATWG didn't think this would be an issue by when they designed the API, I really have no idea. Returning 204 or 404 with no body is a common situation on the Internet and I don't think the server isn't supposed to make up imaginary content just because the client is requesting a specific content-type.

I guess this library could have a fix for us using it.

@alysbrooks
Copy link
Member

I could see a case for a wrapper library like lambdaisland/fetch catching the error or even rethrowing a more reasonable one, but that would be an expansion to this library's scope. The code would be trivial, but semantically it would be a bit of a shift.

There is one place where we catch exceptions: https://github.com/lambdaisland/fetch/blame/926fccabfebced3340548f33e41a72b159d97678/src/lambdaisland/fetch.cljs#L106. So perhaps it wouldn't be too much of a change?

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

3 participants