How to parse an API response in Haskell Servant where content type header is not present?

81 Views Asked by At

I am calling a 3rd Party API in Servant which doesn't specify a content-type header in the response. The response is just a line of text. Servant is failing to parse this response and throwing error response.

What would be the best way to either convert this to a Text or a defined type?

1

There are 1 best solutions below

3
K. A. Buhr On

When a servant client processes a response with no Content-Type header, it treats it like Content-Type: application/octet-stream. To accept this content type, your API endpoint should use OctetStream, instead of PlainText, for the list of accepted content types.

So, your endpoint should look something like this:

type Api = "hello" :> Get '[OctetStream] ByteString

and your handler can return the raw ByteString:

getHello :: ClientM ByteString
getHello = client (Proxy @Api)