I am attempting to make my first Phoenix HTTP request using HTTPoison but it is not working and I cannot figure out why.
If I create a basic project with HTTPoison in it, then go to iex
on this project, I type in the following commands:
userPass = "user:pass"
base64UserPass = Base.encode64(userPass)
authString = "Basic " <> base64UserPass
headers = ["Authorization": authString, "Content-Type": "application/json"]
headers = [{"Authorization", authString}, {"Content-Type", "application/json"}]
body = [language: "sql", command: "select from Profile"]
body = [{"language", "sql"}, {"command", "select from Profile"}]
url = "http://serveraddress.amazonaws.com:2480/api/v1/command/mydb"
HTTPoison.post(url, body, headers)
I am not sure which syntax I should be using for the headers or body out of those options, but either way it doesn’t even seem to get that far as I get:
** (ArgumentError) errors were found at the given arguments:
* 1st argument: not an iodata term
:erlang.iolist_to_binary([{"language", "sql"}, {"command", "select from Profile"}])
(hackney 1.20.1) /deps/hackney/src/hackney_request.erl:352: :hackney_request.handle_body/4
(hackney 1.20.1) /hackney/src/hackney_request.erl:87: :hackney_request.perform/2
(hackney 1.20.1) /deps/hackney/src/hackney.erl:378: :hackney.send_request/2
(httpoison 2.2.1) lib/httpoison/base.ex:888: HTTPoison.Base.request/6
iex:22: (file)
I have searched for info on what I’m doing wrong.
I found this thread that says I should use \
to escape the quotes or use Jason.encode
somehow to circumvent this issue, but I could not figure out how to do this:
https://elixirforum.com/t/1st-argument-not-an-iodata-term-error-for-multilingual-text/48225
I found this but that suggested I am not doing anything entirely wrong I can see:
https://stackoverflow.com/questions/59224740/httpoison-doesnt-accept-this-custom-header
I tried:
body = Jason.encode(%{"language": "sql", "command": "select from Profile"})
headers = Jason.encode(%{"Authorization": authString, "Content-Type": "application/json"})
But that didn’t work either as it said it was bad data. So I don’t actually know what the problem is or how to fix it. The request is not being rejected by the server, but rather it is just giving Elixir errors without sending as I can’t find the right syntax.
Any help? Thanks.