sriki

sriki

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”.

Showing Posts 1 to 8

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.

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 #2
sriki

sriki OP

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.

raichuk

raichuk

I think that the proble is that you need to use inspect, like this:
{:ok, response} -> "response: #{ inspectresponse}"

guljarpd

guljarpd

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

pixare40

pixare40

This saved me as well. Thi was my exact issue

karan

karan

mine too

zoom6x

zoom6x

thanks a lot. it worked perfectly

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews