Skip to content

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

Open

Description

@knubie

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.

Activity

  1. simongray commented on May 19, 2023

    @simongray
    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.

  2. alysbrooks commented on Jul 17, 2023

    @alysbrooks
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions