HTTPoison to make a post request with Authorization in header.

5.4k views Asked by At

I am trying to make a http post request with HTTPoison.

I want to pass json data with header, which contains "Authorization": Bearer #{token}.

In order to achieve that, I have tried,

headers = [{"Authorization": "Bearer #{token}"}, {"Content-Type", "application/json"}] 
body = 
 %{
  id: id,
  name: name,
  ...      
 }
HTTPoison.post(url, body, headers)

But it triggers a syntax error that syntax error before: "Authorization". And I have been searching for right syntax for headers but still no luck..

What is correct syntax for headers ?

Thanks in advance..

1

There are 1 answers

1
Paweł Dawczak On BEST ANSWER

I believe, the correct syntax should be as follows:

headers = ["Authorization": "Bearer #{token}", "Content-Type": "application/json"] 

or, if you prefer the "tuple" way of defining keyword, this would be the equivalent:

headers = [{:"Authorization", "Bearer token"}, {:"Content-Type", "application/json"}]

Hope it helps!