Search code examples
haskellelmservant

How to specify body media type for Elm post request?


I am trying to make a post request using Elm and get the following 'Unsupported Media Type' error output by my server.

POST /users/1/badges
  Request Body: M09991
  Accept: */*
  Status: 415 Unsupported Media Type 0.003431047s

My (servant) server is set to use PlainText to receive the body and I wonder if I am specifying this content type properly in Elm. The code of my post request in Elm is as follows.

postUserBadge : ServerConfig.AdminContext -> Int -> String -> Cmd Msg
postUserBadge context userId licenseNumber =
    Http.send PostUserBadge <|
        postRequest context.baseContext
                    ("/users/" ++ toString userId ++ "/badges")
                    (Http.stringBody "text/plain" licenseNumber)
                    decodeUserBadge

There I use Http.stringBody "text/plain" to specify the request body's content type. The string "text/plain" I got from wikipedia's Media type page; I have not found how to specify Media type in Elm's documentation.

Is this the correct way to specify the plaintext media type in this Elm post request? Is there some other error in my code causing this Http 415 error?


Solution

  • The servant documentation for PlainText indicates that you should send text/plain;charset=utf-8 as the MIME type instead of text/plain.