Skip to content

Impossible to specify "Content-Type: application/json;charset=utf-8" when using Http.jsonBody #75

@Janiczek

Description

@Janiczek

I am developing a REPL-like application for the APL language. It uses UTF-8 characters and the server API requires me to specify the charset:

Content-Type: application/json;charset=utf-8

Otherwise I get output like:

      (+⌿÷≢),¯1+?1000 1000⍴2
NOT PERMITTED: Illegal token
      (+â �¿Ã ·â �¢), ¯1+?1000 1000 â �´2

(The last line shows the garbled output.)

I can't do

Http.request
    { method = "POST"
    , headers = [ Http.header "Content-Type" "application/json;charset=UTF-8" ]
    , url = "https://tryapl.org/Exec"
    , body = Http.jsonBody (encodeStateAndInput model.state model.input)
    , expect = Http.expectJson ReceivedResponse stateAndOutputDecoder
    , timeout = Nothing
    , tracker = Nothing
    }

because Http.jsonBody adds its own Content-Type header and this gets sent:

Content-Type: application/json;charset=UTF-8 application/json

I can't keep headers empty and send Content-Type: application/json since that will still result in the garbled output.

So far the only way to get out of this situation is to use stringBody instead of jsonBody and encode the JSON myself.

I believe the correct solution would be to check the Content-Type headers and use only the user-given one if there are multiple, instead of joining it and the jsonBody-given one into one string.

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