Search code examples
http-headerselm

How to set a custom content type in an Elm HTTP request


I am exploring Elm for use in a single-page application with a REST backend that follows the JSON:API specification. Hence, all resources have the content type application/vnd.api+json instead of the usual application/json. What is the recommended approach to configure this content type in an Elm SPA?

To my understanding, when I use Http.expectJson as expected response type, elm/http will automatically set the request header content type to application/json. How can I change that? Do I need to write my own request functions that wrap Http.request? Is there a simpler, less invasive way to do it?


Solution

  • Based on your description, it sounds like you are trying to set the Content-Type header of the HTTP request that you are sending to your server. The Http.expectJson function doesn't modify the request header.

    If you wanted to change the request header, you can use Http.stringBody to manually set the Content Type being sent to the server. Here's a simple example of such a helper function.

    postApiJson : String -> (Result Http.Error a -> msg) -> Decoder a -> Json.Encode.Value -> Cmd msg
    postApiJson url msg decoder body =
        Http.post
            { url = url
            , expect = Http.expectJson msg decoder
            , body = Http.stringBody "application/vnd.api+json" (Json.Encode.encode 0 body)
            }