Chapter 8. Using the HTTP Client
Sometimes you need not to create an HTTP service but to consume
one—perhaps to use a RESTful service or other API, or to load-test a server
by hitting it with lots of requests at once. Erlang provides an HTTP client
API that lets you do this. You can find the manual page for this module at
httpc module is part of the standard Erlang distribution
and does not have to be installed separately.
The first thing to do before using the
httpc module is to start the
inets service by calling
application:start(inets). If you are running an
application this can be done from the .erlang file or
from the command line in testing. If you do not start
httpc will not work correctly.
If there are some options that must be set for all calls then you can
use the function
httpc:set_options/2. There are a number
of options that can be set here, including all of the standard ones you
would expect. Of particular note is the
max_sessions option, which defaults to 2. In
addition, if you need to set a proxy server you can do it here with the
Proxy option. When calling
set_option it will return
There are several other HTTP client packages for Erlang that provide
more features, including
lhttpc. You can find both online.
Making a Request
There are four functions available to make an HTTP request that run from one to five parameters (there is no function ...