chavenor
chavenor

Reputation: 47

HTTPoison doesn't accept this custom header

I have a curl request of the below that works but when I translate it into the below I get the following error what is wrong with this header - I feel I've done it every possible way.

url = "https://content.dropboxapi.com/2/files/download"
header = [ {"Authorization", "Bearer #{token}"}, {"Dropbox-API-Arg", "{\"path\": \"/dropboxtest/image.jpg\"}"} ]
HTTPoison.post(url=url, header=header)

** (ArgumentError) argument error
    :erlang.iolist_to_binary([{"Authorization", "Bearer <removed>"}, {"Dropbox-API-Arg", "{\"path\": \"/dropboxtest/image.jpg\"}"}])
    (hackney) /Users/casey/Dropbox/BOS Sales LLC/backyardmicro/projects/k-9dryers/software/connectk9/deps/hackney/src/hackney_request.erl:348: :hackney_request.handle_body/4
    (hackney) /Users/casey/Dropbox/BOS Sales LLC/backyardmicro/projects/k-9dryers/software/connectk9/deps/hackney/src/hackney_request.erl:83: :hackney_request.perform/2
    (hackney) /Users/casey/Dropbox/BOS Sales LLC/backyardmicro/projects/k-9dryers/software/connectk9/deps/hackney/src/hackney.erl:376: :hackney.send_request/2
    (httpoison) lib/httpoison/base.ex:796: HTTPoison.Base.request/6

Upvotes: 1

Views: 408

Answers (1)

Jaffa
Jaffa

Reputation: 12719

The function you want to call is documented here.

It gets the form:

HTTPoison.post(url, body, headers \\ [], options \\ []) 

So the first argument shall be the URL, then the body, and then only the headers.

By writing HTTPoison.post(url=url, header=header), note that the url=url and header=header does not really have a meaning for what you're trying to achieve, simply write, when you have an empty body:

HTTPoison.post(url, "", header)

(As a matter of recommendation, I'd rename header to headers too if I were you)

Upvotes: 2

Related Questions