Make a HTTP GET Request using cohttp-lwt-unix
Task
Web Programming / HTTP Clients / Make a HTTP GET Request
Opam Packages Used
- cohttp-lwt-unix Tested with version: 5.3.0 — Used libraries: cohttp-lwt-unix
- cohttp Tested with version: 5.3.1 — Used libraries: cohttp
- lwt Tested with version: 5.7.0 — Used libraries: lwt, lwt.unix
Code
let ( let* ) = Lwt.bind
This function performs an HTTP GET request to a given URL and handles the response. It returns a Result type: Ok with the response body if successful, or Error with an error message.
Step by step:
- Takes a URL as a string parameter
- Uses
Cohttp_lwt_unix.Client.get
to make the HTTP request (let*
is used because this is an asynchronous operation within theLwt
monad) - Extracts the HTTP status code from the response
- If the status code indicates success (2xx):
- Converts the response body to a string
- Returns Ok with the body string
- If not successful:
- Returns Error with the HTTP error message
let http_get url =
let* (resp, body) =
Cohttp_lwt_unix.Client.get (Uri.of_string url)
in
let code = resp
|> Cohttp.Response.status
|> Cohttp.Code.code_of_status in
if Cohttp.Code.is_success code
then
let* b = Cohttp_lwt.Body.to_string body in
Lwt.return (Ok b)
else
Lwt.return (Error (
Cohttp.Code.reason_phrase_of_code code
))
This is the main program that:
- Runs our
http_get
function with ocaml.org as the target - Uses
Lwt_main.run
to execute our async code - Handles the result:
- If
Error
: prints the URL and error message - If
Ok
: prints the URL and the received HTML content
- If
- All printing is done inside the
Lwt
monad (hence theLwt.return ()
)
let () =
let url = "https://ocaml.org" in
Lwt_main.run (
let* result = http_get url in
match result with
| Error str ->
Printf.printf "%s:fail\n" url;
Printf.printf "Error: %s\n" str;
Lwt.return ()
| Ok result ->
Printf.printf "%s:succed\n" url;
print_string result;
Lwt.return ()
)
Discussion
The cohttp
package provides a client and a server implementation of HTTP(S).
Note: Lwt
is OCaml's library for handling asynchronous operations,
similar to Promises in JavaScript
The package lwt_ppx
can be used for syntactic
sugar (let%lwt
instead of having to define a custom
shorthand for Lwt.bind
).