Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPoison to make a post request with Authorization in header.

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

like image 771
D.R Avatar asked Mar 07 '23 04:03

D.R


1 Answers

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!

like image 143
Paweł Dawczak Avatar answered Apr 25 '23 15:04

Paweł Dawczak