Error on using post with HTTPoison

Hi ,

I’m trying to use HTTPoison for a POST request . The problem is no matter what I do I get: no case clause matching {:ok :request etc. }. Event when I don’t use any matches.

Code:

def fetch_labels(_img_base64) do

    case HTTPoison.post(
      "#{img_url()}#{app_id()}", get_labeling_request(), [{"Content-Type", "application/json"}]) do
        
        {:ok, %HTTPoison.Response{body: body, status_code: 200}} ->
            IO.puts body
        {:error, %HTTPoison.Error{reason: reason}} ->
            IO.inspect reason
        
    end

end

defp get_labeling_request do

Poison.encode(%{"requests": [
    %{"image": %{"content": "c"}, "features": %{"type": @label_detection }}
]})

end

Error:

[error] Task #PID<0.424.0> started from ImgClassifier.Supervisor terminating
** (CaseClauseError) no case clause matching: {:ok, “{“requests”:[{“image”:{“content”:“c”},“features”:{“type”:“LABEL_DETECTION”}}]}”}
(hackney) /home/g/Dropbox/Work/Jarvis/Web/jarvis_api_umbrella/deps/hackney/src/hackney_request.erl:312: :hackney_request.handle_body/4
(hackney) /home/g/Dropbox/Work/Jarvis/Web/jarvis_api_umbrella/deps/hackney/src/hackney_request.erl:81: :hackney_request.perform/2
(hackney) /home/g/Dropbox/Work/Jarvis/Web/jarvis_api_umbrella/deps/hackney/src/hackney.erl:358: :hackney.send_request/2
(httpoison) lib/httpoison/base.ex:439: HTTPoison.Base.request/9
(img_classifier) lib/google_img_classifier.ex:27: ImgClassifier.Google.fetch_labels/1
(img_classifier) lib/google_img_classifier.ex:14: ImgClassifier.Google.fetch/4
(elixir) lib/task/supervised.ex:88: Task.Supervised.do_apply/2
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: &ImgClassifier.Google.fetch/4
Args: ["", #Reference<0.3692720699.547094530.92623>, #PID<0.422.0>, 10]

If i use directly:
HttpPoison.post(“https://vision.googleapis.com/v1/images:annotate?key=xxxxxxx”, “{“requests”:[{“image”:{“content”:“c”},“features”:{“type”:“LABEL_DETECTION”}}]}”, [{“Content-Type”, “application/json”}])
things are looking good. The command is populated with parameters exactly like the ones used in code as I got them via logger.
Elixir 1.6 (compiled with OTP 19)
OTP 20
Dependencies

{:poison, “~> 2.0”},
{:uuid, “~> 1.1”},
{:httpoison, “~> 1.0”}

Any help for figuring out what I do wrong is greatly appreciated.

Thank you

Which line is lib/google_img_classifier.ex:27 ?

case HTTPoison.post(

Httpoison wants a string as body AFAIR, but you are passing in an ok-tuple, because you are using poisons result directly. You need to unwrap it first.

5 Likes

Thank you NobbZ. Indeed the Poison.encode returns a tuple. In my mind it was returning a string.