6

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..

D.R
  • 829
  • 4
  • 16
  • 30

1 Answers1

7

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!

Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
  • 3
    If you're using tuples, do this: [ {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"Authorization", "Bearer #{token}"} ] – Mark Wilbur Nov 26 '20 at 19:30