Rendering JSON from remote URL source results in strangely formatted response

I am trying to load JSON data from remote server and sending back to user inside custom controller’s action, like this:

  def order(conn, _params) do
    headers = ["Accept": "Application/json; Charset=utf-8"]
    case HTTPoison.get('http://200.1.1.100/api/orders', headers) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        IO.puts body
        json conn, body
      {:ok, %HTTPoison.Response{status_code: 404}} ->
        IO.puts "Not found :("
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.inspect reason
    end
  end

Instead of getting this:

[
  {
    "DocDate": "2017-11-14T00:00:00",
    "DocEntry": 4908,
    "U_ORDER_STATUS": 0
  },
  {
    "DocDate": "2017-11-14T00:00:00",
    "DocEntry": 4907,
    "U_ORDER_STATUS": 0
  }
]

I get:

"[\u0000\r\u0000\n\u0000 \u0000 \u0000{\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000D\u0000o\u0000c\u0000D\u0000a\u0000t\u0000e\u0000\"\u0000:\u0000 \u0000\"\u00002\u00000\u00001\u00007\u0000-\u00001\u00001\u0000-\u00001\u00004\u0000T\u00000\u00000\u0000:\u00000\u00000\u0000:\u00000\u00000\u0000\"\u0000,\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000D\u0000o\u0000c\u0000E\u0000n\u0000t\u0000r\u0000y\u0000\"\u0000:\u0000 \u00004\u00009\u00000\u00008\u0000,\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000U\u0000_\u0000O\u0000R\u0000D\u0000E\u0000R\u0000_\u0000S\u0000T\u0000A\u0000T\u0000U\u0000S\u0000\"\u0000:\u0000 \u00000\u0000\r\u0000\n\u0000 \u0000 \u0000}\u0000,\u0000\r\u0000\n\u0000 \u0000 \u0000{\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000D\u0000o\u0000c\u0000D\u0000a\u0000t\u0000e\u0000\"\u0000:\u0000 \u0000\"\u00002\u00000\u00001\u00007\u0000-\u00001\u00001\u0000-\u00001\u00004\u0000T\u00000\u00000\u0000:\u00000\u00000\u0000:\u00000\u00000\u0000\"\u0000,\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000D\u0000o\u0000c\u0000E\u0000n\u0000t\u0000r\u0000y\u0000\"\u0000:\u0000 \u00004\u00009\u00000\u00007\u0000,\u0000\r\u0000\n\u0000 \u0000 \u0000 \u0000 \u0000\"\u0000U\u0000_\u0000O\u0000R\u0000D\u0000E\u0000R\u0000_\u0000S\u0000T\u0000A\u0000T\u0000U\u0000S\u0000\"\u0000:\u0000 \u00000\u0000\r\u0000\n\u0000 \u0000 \u0000}\u0000\r\u0000\n\u0000]\u0000"

Any idea what might be going on here?

Looks like it were UTF16 encoded. Also you are encoding a binary as JSON. You need to decode it first.

1 Like

Or skip decoding and encoding and just send it as is with send_resp/3.

  def order(conn, _params) do
    headers = ["Accept": "Application/json; Charset=utf-8"]
    case HTTPoison.get("http://200.1.1.100/api/orders", headers) do
      {:ok, %HTTPoison.Response{status_code: status_code, body: body}} ->
        send_resp(conn, status_code, body)
      # ...
    end
  end
2 Likes

Save file as appears in browser. The response is not displayed on screen (yet the content of the saved file is 100% fine)

Here is the solution:

conn
|> put_resp_content_type("Application/json", "utf-16")
|> send_resp(200, body)

Thank you @idi527 and @NobbZ

1 Like