mikejm

mikejm

Can't get basic HTTPoison.post working? "1st argument: not an iodata term"

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://forum.elixirforum.net/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.

Marked As Solved

mikejm

mikejm

Thank you for your advice. Between that and this example I found:

https://stackoverflow.com/questions/37385314/httpoison-insert-body-parameters-in-elixir

I was able to figure it out. The correct approach is:

userPass = "user:pass"
base64UserPass = Base.encode64(userPass)
authString = "Basic " <> base64UserPass
url = "http://serveraddress.amazonaws.com:2480/api/v1/command/mydb"
headers = ["Authorization": authString, "Content-Type": "application/json"] 
headers = [{"Authorization", authString}, {"Content-Type", "application/json"}] 
body = Jason.encode!(%{language: "sql", command: "select from Profile"})
HTTPoison.post(url, body, headers)

So I just needed to “encode” the body and the headers was supposed to be used in either format above. Worked and returned correct from server. Thanks again.

Perhaps this will be helpful to other people, but I find of all programming languages I’ve gone into, Elixir/Pheonix has the most opaque documentation. Most of the Hex Docs (like HTTPoison) have no examples of any use that would show things work.

I’m not sure why that is. I suspect there are very few “beginners” or “intermediate” programmers using Elixir in any scenario so everything is written with the notion you already know what you’re supposed to do intuitively.

Also Liked

D4no0

D4no0

First of all the body parameter should be a string, not a list.

Second thing is that Jason.encode/2 returns response in format {:ok, binary}, you either pattern match on response or use the bang variant Jason.econde!/2.

The third thing is that jason doesn’t know how to encode tuples, since json format doesn’t have this concept, so you have to use a map instead:

body = %{language: "sql", command: "select from Profile"}
body_binary = Jason.encode!(body)
D4no0

D4no0

This is absolutely not true. Elixir from conception was created to have a good documentation infrastructure in mind and all mature libraries have great documentation. It is a lot to take in as a beginner as @benwilson512 said, because there are a lot of things to unpack.

A short description on how you should read documentation is the following:

  1. Open the function documentation on hexdocs for HTTPoison.post/4
  2. Look at the specs of the function, in our case we have the following:
post(url, body, headers \\ [], options \\ [])

# spec
post(binary(), any(), headers(), Keyword.t()) ::
{:ok,
 HTTPoison.Response.t()
 | HTTPoison.AsyncResponse.t()
 | HTTPoison.MaybeRedirect.t()}
| {:error, HTTPoison.Error.t()}
  1. For headers, if you follow the link you will get the following spec:
headers() ::
  [{atom(), binary()}]
  | [{binary(), binary()}]
  | %{required(binary()) => binary()}
  | any()

This states clearly that you can create headers in the following formats:

["Authorization": authString, "Content-Type": "application/json"] 
[{"Authorization", authString}, {"Content-Type" "application/json"}]
%{"Authorization" => authString, "Content-Type" => "application/json"}  
  1. Now the body parameter is set as any, however if you follow the description of the parameter from the function, you will be redirected to request/5 and then to HTTPoison.Request where there is a clear description of the format:

:body:
binary, char list or an iolist
{:form, [{K, V}, …]} - send a form url encoded
{:file, “/path/to/file”} - send a file
{:stream, enumerable} - lazily send a stream of binaries/charlists

Could this documentation be improved? definetly, more examples could be added also, nothing is perfect
Is it missing vital documentation? absolutely no

And that is all you need to know, as long as you respect these specs, you requests should work as expected.

My advice is that if you don’t want to keep shooting yourself in the leg, take some time to learn elixir syntax, preferably read a book that covers the philosophy of the language, then dive into more complex things like phoenix, as that is a full fledged mature framework that is not meant to be beginner friendly, but to get things done.

arcyfelix

arcyfelix

One very important thing - Elixir uses snake case naming. If you don’t follow that, formatting might have issues.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think a key challenge is that each library tends to be unopinionated about which other libraries you use. HTTPoison raised the error, but the mistake was earlier and involved other libraries.

Elixir is certainly a language that favors a good grasp of fundamentals before stitching together different libraries.

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement