How to make a simple GET request in OCaml?

I'm trying to do something that should be simple: make a GET request to the url. However, when I look for examples of how to do this, I often end up almost gibberish, like this .

Does anyone know how to make a simple HTTP request using OCaml? I am new to OCaml with some Haskell exp.

Note:

A solution using the lowest possible level of OCaml would be ideal. I saw the Cohttp library Cohttp , but I'm more interested in the native (?) HTTP OCaml lib or something like that.

In response to @antron , a solution using low-level native OCaml would be much appreciated. I am convinced that this will be related to the Unix library. But if there is another solution that is not related to third-party libraries, that would be very welcome.

+6
source share
2 answers

Use the Cohttp library. See Client Example .

Corresponding line:

 Cohttp_lwt_unix.Client.get (Uri.of_string "http://www.reddit.com/") 

This gives you a pair (response, body) inside the Lwt monad. response is basically a record, and body is a stream. The rest of the example is just some interesting snippets.

+5
source

For low-level Unix programming in OCaml (even if you know little about it), I recommend the excellent Unix System Programming book in OCaml . He will tell you how to write the desired customer.

0
source

All Articles