Looks like you’re calling httpc:request/2
with the wrong kind of second argument. It expects url()
and then profile() | pid()
(i.e. an atom or a pid for arg 2), in which the second argument Profile
is (I think) a way to specify a connection profile other than the default service profile created by inets:start/0
. You’re giving it the HTTPOptions
structure required by httpc:request/4
, so function clause request/2
isn’t matching, hence the error.
It’s all specified in the httpc doc but the profile stuff is definitely a bit abstruse There are better examples on the HTTP Client page.
Basically though, if you’ve already got the inets
app running then you don’t have to specify a profile, so you can just call httpc:request/4
like httpc:request(get, {URL, [{"Authorization", API_KEY}]}, [], [])
.
If you don’t already have inets
service running, you can do this:
ok = ssl:start(), % necessary if your URL is https://...
ok = inets:start(),
URL="https://www.example.com",
API_KEY="sdkfjhsdf",
Response = httpc:request(get, {URL, [{"Authorization", API_KEY}]}, [], []).
{ok,{{"HTTP/1.1",200,"OK"},
[{"cache-control","max-age=604800"},
{"date","Tue, 22 Nov 2022 18:04:41 GMT"},
{"age","201875"},
{"etag","\"3147526947+ident\""},
{"server","ECS (dcb/7EEC)"},
{"vary","Accept-Encoding"},
{"content-length","1256"},
{"content-type","text/html; charset=UTF-8"},
{"expires","Tue, 29 Nov 2022 18:04:41 GMT"},
{"last-modified","Thu, 17 Oct 2019 07:18:26 GMT"},
{"x-cache","HIT"}],
"<!doctype html>\n<html>\n[...]</html>\n"}}
(You’ll maybe also get a cacertfile
warning in there but that’s beyond this question.)
You might also want to have a look at hackney HTTP client library. It’s a bit friendlier than httpc
, there’s a hackney:request/3
which just takes Method, URL, Headers
which seems like what you want to do.
Good luck