httpc:request hangs with an HTTPS URL

16 Oct 2023 13:53 erlang

If you use Erlang/OTP’s provided HTTP client as follows, it hangs:

{ok, _} = application:ensure_all_started(inets),
{ok, {{_, 200, _}, Headers, Body}} =
    httpc:request(get, {"https://www.erlang.org", []}, [{ssl, [{verify, verify_none}]}], []).

This is covered in the documentation, but it’s not obvious:

If the scheme https is used, the SSL application must be started.

…and it would be nicer if it didn’t hang.

To fix it, change your code like this:

{ok, _} = application:ensure_all_started(inets),
{ok, _} = application:ensure_all_started(ssl),
{ok, {{_, 200, _}, Headers, Body}} =
    httpc:request(get, {"https://www.erlang.org", []}, [{ssl, [{verify, verify_none}]}], []).

If you’re using OTP 26 or newer, application:ensure_all_started/1 takes a list, so this also works:

{ok, _} = application:ensure_all_started([inets, ssl]),
% etc.