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 http://www.erlang.org/doc/man/httpc.html. The 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 inets:start() or 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 inets, httpc will not work correctly. [12]

If there are some options that must be set for all calls then you can use the function httpc:set_options/1 or 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 ok or {error, Reason}.

Note

There are several other HTTP client packages for Erlang that provide more features, including ibrowse and 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 ...

Get Building Web Applications with Erlang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.