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..
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With