Binary or text binary

Hi, guys
I’m getting url response via HTTPoison. When handle response how to determine type of the response. Is it image(binary file) or is text?

If you do not know the expected type of the response in advance it should be specified in thte content-type header of the response.

1 Like

Yes I know for Content Type, there are too many types :slight_smile: . I was thinking to check for non printing characters. Is this suitable?

You can construct imagefiles that look like plaintext but are still valid and therefore to consider binary files, or consider the Netbpm graphic format. It has 3 representations which are totally blown regarding to the filesize but do only consist of printable characters.

Usually you can assume that the media types text/* are text, everything else is binary. If you know other types that you want to treat as text, enable them explicitely.


Yes you are right. I wanted a little cheat :slight_smile:

Image files often have a simple header you can use to make an educated guess as to the file type. Something like:

file_type = case file_content do
  << 137, "PNG", 13, 10, 26, 10, _rest :: binary >> -> :png
  << _::binary-size(6), "Exif", _rest :: binary >> -> :jpeg
  << _::binary-size(6), "JFIF", _rest :: binary >> -> :jpeg
  << "GIF", "87a", _rest :: binary >> -> :gif
  << "GIF", "89a", _rest :: binary >> -> :gif
  content -> if String.printable?(content), do: :text, else: :unknown

This is more complicated, than a Content-Type. If I got wrong content-type this can be solution. Because A-tag href can point me to bin, zip, tar or whatever extension is invented :slight_smile:

Content-Type is definitely the canonical approach for sure.


@kip that’s a nice little snippet! Do you know if it’s wrapped up in a library or something somewhere?

I once hacked a quick wrapper 'round the linux tool file,