I'm downloading an image with HTTPoison but I cannot figure out how to get the image from the response's body. I need to put it to annotate_image as a base64 encoded string to pass to another API. However, I cannot figure out what the body is. Even IO.puts body throws an error and it says I'm calling it with 3 arguments and I can clearly see that the image is one of these arguments but I cannot extract it in my annotate_image function. I have tried "body[ 1 ]", body(1), body.1, body.get(1) and other variations, nothing works. How can I figure out what type of structure body is and what its member variables/params are?
def get_image(url) do
finishedUrl = Enum.join(url, "/")
finishedUrl = String.replace(finishedUrl, "/", "//", global: false)
IO.puts "Printing url part 2"
IO.puts finishedUrl
case HTTPoison.get(finishedUrl) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
annotate_image(body)
{:ok, %HTTPoison.Response{status_code: 404}} ->
IO.puts "Not found :("
{:error, %HTTPoison.Error{reason: reason}} ->
IO.inspect reason
end
IO.puts "get_image ended here"
end
The body is just a binary, which, assuming you're indeed downloading an image file here, is going to have the contents of the file in it. You can verify that by saving it to a file with a proper extension (.png
, say, but that depends on the actual file format) and opening it on your computer. Depending on what you want to do with the image you might need an image manipulation library to process it in your program.