sriki

sriki

Protocol String.Chars not implemented for

I got the error mentioned in the title while running the following code. The weird part is I did get the response I expected but with this message attached.

defmodule Kodi.Worker do
def get_response() do
    result = url_for() |> HTTPoison.get |> parse_response
    case result do
        {:ok, response} -> "response: #{response}"
        :error -> "query error"
    end
end

defp url_for() do
    "http://192.168.0.104:8080/jsonrpc?request={%22jsonrpc%22:%20%222.0%22,%20%22id%22:%201,%20%22method%22:%20%22Playlist.GetPlaylists%22}"
end

defp parse_response({:ok, %HTTPoison.Response{body: body, status_code: 200}}) do
    body |> JSON.decode! |> filter_response
end

defp parse_response(_) do
    :error
end

defp filter_response(json) do
    try do
        #response = (json["result"])
        #{:ok, response}
        {:ok, json}
    rescue
        _ -> :error
    end
end
end

Any idea?

EDIT:
Error message:
** (Protocol.UndefinedError) protocol String.Chars not implemented for %HTTPoison.Response{body: “{"id":1,"jsonrpc":"2.0","result":[{"playlistid":0,"type":"audio"},{"playlistid":1,"type":"video"},{"playlistid":2,"type":"picture"}]}”, headers: [{“Content-Length”, “133”}, {“Content-Type”, “application/json”}, {“Date”, “Wed, 04 May 2016 05:00:55 GMT”}], status_code: 200}
(elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) lib/string/chars.ex:17: String.Chars.to_string/1
(kodi) lib/worker.ex:5: Kodi.Worker.get_response/0

It looks like you are correct. I might be treating the response as string when it’s not. I will look into it again to figure out how to fix it. Thanks.

I made a mistake in my code. I am fixing it and update the thread “soon”.

Most Liked

hubertlepicki

hubertlepicki

You are not getting this error caused by the code above.

What may be happening is that you are getting a JSON response out from the get_response() function, and later on treat it as a string, while it is Map.

I usually get this error message when I try to use IO.puts instead of IO.inspect to print something out to console that is not a string.

Fuller code example would be better, along with full error message (you cut it off in most interesting part, before the module name).

31
Post #3
guljarpd

guljarpd

#hubertlepicki
Thanks,
Your post saved my lot of time, same issue I was also facing.

NobbZ

NobbZ

It would have been much easier if you had posted the full errormessage including the module for that String.Chars is not implemented and the line number it is complaining about.

So I have to guess for now where the message does come from, and the sole possbility I do see for now is this line: {:ok, response} -> "response: #{response}". response is of type HTTPotion.Response or HTTPotion..AsyncResponse and after a quick look up in the sources I wasn’t able to find a defimpl for both of them and they don’t seem to have Inspect implemented either (there was not a single defimpl in the repository according to github search).

So If that is really the line causing you trouble, You need to get the information you want to have in the string in another way.

sriki

sriki

I missed the “!” in the above line. With that added, I still got an error message but it’s more about the json data structure and how it’s being parsed. I will deal with it. Thanks for pointing in the right direction and apologies for cutting off the full error message.

pixare40

pixare40

This saved me as well. Thi was my exact issue

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement