Skip to content

Commit

Permalink
z_url_fetch: add option to pass extra http headers (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Jan 29, 2024
1 parent beb4d3c commit f3f0346
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/z_url_fetch.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
% @author Marc Worrell
%% @copyright 2014-2024 Marc Worrell
%% @doc Fetch (part of) the data of an Url, including its headers.
%% @end

%% Copyright 2014-2024 Marc Worrell
%%
Expand Down Expand Up @@ -66,6 +67,7 @@
| {user_agent, binary() | string()}
| {language, atom()}
| {content_type, binary() | string()}
| {headers, [ {binary()|string(), binary()|string()} ]}
| insecure.

-type fetch_result() :: {ok, {string(), list({string(), string()}), pos_integer(), binary()}} | {error, term()}.
Expand Down Expand Up @@ -189,7 +191,7 @@ fetch_partial(Method, Url0, Payload, RedirectCount, Max, OutDev, Opts) when is_b
undefined -> "application/octet-stream";
CT -> to_list(CT)
end,
Headers = [
Headers0 = [
{"Accept", Accept},
{"Accept-Encoding", "identity"},
{"Accept-Charset", "UTF-8;q=1.0, ISO-8859-1;q=0.5, *;q=0"},
Expand All @@ -202,6 +204,19 @@ fetch_partial(Method, Url0, Payload, RedirectCount, Max, OutDev, Opts) when is_b
undefined -> [];
Auth -> [ {"Authorization", to_list(Auth)} ]
end,
Headers = case proplists:get_value(headers, Opts) of
undefined ->
Headers0;
[] ->
Headers0;
Hs ->
Hs1 = lists:map(
fun({K,V}) ->
{z_convert:to_list(K), z_convert:to_list(V)}
end,
Hs),
Headers0 ++ Hs1
end,
Request = case Method of
get -> {Url, Headers};
delete when Payload =:= <<>> -> {Url, Headers};
Expand Down

0 comments on commit f3f0346

Please sign in to comment.