spizzy
September 14, 2022, 6:21pm
1
So I want to fetch an API from RapidAPi, they require the following headers
X-RapidAPI-Key
+ X-RapidAPI-Host
.
My process function that kinda “handles” basic http request
@token Application.get_env(:appsfc, :token)
def process_request_headers(headers),
do: headers ++ [Authorization: "Bearer #{@token}", Accept: "application/json"]
while the @token here is the api key.
I tried the following:
def test do
url = Application.get_env(:appsfc, :api_url)
options = [
method: "GET",
headers: [
"X-RapidAPI-Key": "api key here",
"X-RapidAPI-Host": "the url ....com"
]
]
%{body: body} = HTTPoison.Base.get!(url, options)
body
end
however, I got the following error:
** (ArgumentError) errors were found at the given arguments:
* 1st argument: not an iolist term
The typespec requires url, headers \\ [], options \\ []
, but im not sure how to set the headers and options correctly
1 Like
Have you tried inspecting the contents of url
?
By the sound of that error, it seems like it may be nil
.
1 Like
spizzy
September 17, 2022, 3:16pm
4
So I want to fetch an API from RapidAPi, they require the following headers
X-RapidAPI-Key
+ X-RapidAPI-Host
.
My process function that kinda “handles” basic http request
@token Application.get_env(:appsfc, :token)
def process_request_headers(headers),
do: headers ++ [Authorization: "Bearer #{@token}", Accept: "application/json"]
while the @token here is the api key.
I tried the following:
def test do
url = Application.get_env(:appsfc, :api_url)
options = [
method: "GET",
headers: [
"X-RapidAPI-Key": "api key here",
"X-RapidAPI-Host": "the url ....com"
]
]
%{body: body} = HTTPoison.Base.get!(url, options)
body
end
however, I got the following error:
** (ArgumentError) errors were found at the given arguments:
* 1st argument: not an iolist term
The typespec requires url, headers \\ [], options \\ []
, but im not sure how to set the headers and options correctly
You’re passing options
as headers
. If there are multiple optional arguments with default value, your arguments are used for the most left one first, not the most right one.
url = "https://postman-echo.com/get"
HTTPoison.get!(url, [{"foo", "bar"}])
HTTPoison.get!(url, [], [headers: {"foo", "bar"}])
Why that error?
It tries to use headers
as a header key and [{"foo", "bar"}]
as a header value - so tries to convert it to binary.
HTTPoison.get!(url, [headers: [{"foo", "bar"}]])
:erlang.list_to_binary([{"foo", "bar"}])
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney_bstr.erl:36: :hackney_bstr.to_binary/1
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney_headers_new.erl:193: anonymous fn/3 in :hackney_headers_new.to_iolist/1
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney_headers_new.erl:148: :hackney_headers_new.do_fold/3
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney_headers_new.erl:184: :hackney_headers_new.to_iolist/1
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney_request.erl:101: :hackney_request.perform/2
(hackney 1.18.1) /Users/chulki/tmp/ht/deps/hackney/src/hackney.erl:378: :hackney.send_request/2
(httpoison 1.8.2) lib/httpoison/base.ex:846: HTTPoison.Base.request/6
Also HTTPoison.get!
is what you want - not HTTPoson.Base
which is a behaviour, not an implementation.
1 Like
BTW this is a duplicate of HTTP request with special headers - @spizzy please reply to a previous post instead of posting again
spizzy
September 17, 2022, 6:23pm
7
%{body: body} = HTTPoison.get!(url, [], [headers: options])
, im unsure about the options
here, I need to pass the X-RapidAPI-Key
and the X-RapidAPI-Host
there, with the value of an api key, how would I do that
They’re HTTP headers, right? Then pass it as headers:
# using string key
HTTPoison.get!(url, [
{"X-RapidAPI-Key", "api key here"},
{"X-RapidAPI-Host", "the url ....com"}
])
# using symbol key
HTTPoison.get!(url, [
"X-RapidAPI-Key": "api key here",
"X-RapidAPI-Host": "the url ....com"
])
Both works, but I usually prefer to avoid symbol
["a": "b"] == [{:"a", "b"}]
# true
["a": "b"] == [{"a", "b"}]
# false
2 Likes
Moved posts to the original topic.
1 Like