HTTPC body

What is that array of numbers returned in the body? In the browser, it returns JSON. Is there a way to decode/encode it into JSON? Or just better to use a library?

iex()> {:ok, {{'HTTP/1.1', 200, 'OK'}, headers, body}} = :httpc.request(:get, {'https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=1234', []}, [], [])

{:ok,
 {{'HTTP/1.1', 200, 'OK'},
  [
     {'cache-control', 'no-cache'},
     {'date', ''}
 ],
[123, 34, 115, 116, 97, 116, 117, 115, 34, 58, 34, 111, 107, 34, 44, 34, 116,
111, 116, 97, 108, 82, 101, 115, 117, 108, 116, 115, 34, 58, 50, 48, 44, 34,
97, 114, 116, 105, 99, 108, 101, 115, 34, 58, 91, ...]}}

Its the list of bytes forming the body. Probably it contains some bytes that elixir considers unprintable.

The printed prefix is equivalent to: ~c'{"status":"ok","totalResults":20,"articles":['

Perhaps you should specify body_format: :binary in the options list to :httpc.request/4?

2 Likes