A simple wrapper around OCurl.
Find a file
Edwin Török a92d7bbe4c ezcurl: allow POST with non-form data
Currently ezcurl always sets CURLOPT_HTTPPOST, but that means it is not able
to send a custom body that is not a form (e.g. JSON is quite a common use-case
these days).

Add a special case: if the POST params is empty, *and* a 'content' is set, then
just set CURLOPT_POST, but not CURLOPT_HTTPPOST.

Example usage:
```
let contents = Ezjsonm.to_string json in
let config = EZ.Config.(verbose true default) in
match EZ.post ~config ~url ~params:[] ~content:(`String contents) () with
```

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
2023-01-31 16:38:12 +00:00
.github/workflows move to github actions 2021-07-22 10:59:16 -04:00
examples/argiope fix build 2019-09-14 12:30:53 -05:00
src ezcurl: allow POST with non-form data 2023-01-31 16:38:12 +00:00
.gitignore initial commit 2019-09-13 18:22:56 -05:00
argiope.sh feat: add an example, a simple web crawler that looks for deadlinks 2019-09-14 12:25:34 -05:00
dune fix test 2023-01-25 13:43:35 -05:00
dune-project initial commit 2019-09-13 18:22:56 -05:00
ezcurl-lwt.opam prepare for 0.2.2 2023-01-25 13:44:38 -05:00
ezcurl.opam prepare for 0.2.2 2023-01-25 13:44:38 -05:00
LICENSE add license file (MIT) 2019-09-16 12:59:46 -05:00
Makefile chore: modify makefile 2019-09-13 21:32:11 -05:00
README.md update readme 2021-07-22 10:59:50 -04:00

EZCurl build

A simple wrapper around OCurl, for easy tasks around http.

project goals

  • be as simple to use as possible.
  • be as reliable as possible (work is done by cURL and the ocurl bindings anyway).
  • stable API with few dependencies, so that user code compiles without breakage for a long time.

Installation

  • for the synchronous library: opam install ezcurl
  • for the lwt-baed library: opam install ezcurl-lwt (depends on ezcurl)

Usage

A small web crawler can be found in examples/argiope. It's very incomplete and naive but demonstrates basic usage of Ezcurl_lwt.get.

Synchronous API

The library lives in a module Ezcurl, which wraps Curl.t with functions such as get that combine many different low level API calls into one. It also follows redirections by default, and returns a Ezcurl.response object that contains the body, headers, and error code.

# #require "ezcurl";;
# let url = "https://curl.haxx.se/";;
val url : string = "https://curl.haxx.se/"
# let res = Ezcurl.get ~url ();;
...
# let content = match res with Ok c -> c | Error (_,s) -> failwith s;;
val content : Ezcurl_core.response =
...

# content.Ezcurl.code;;
- : int = 200

It is also possible to create a client with Ezcurl.make() and re-use it across calls with the optional parameter client.

Lwt API

Using ezcurl-lwt, a module Ezcurl_lwt becomes available, with functions that are similar to the ones in Ezcurl but are non blocking. This makes it easy to run several queries in parallel:

# #require "ezcurl-lwt";;
# let urls = [
  "https://en.wikipedia.org/wiki/CURL";
  "https://en.wikipedia.org/wiki/OCaml";
  "https://curl.haxx.se/";
  ];;
val urls : string list =
  ["https://en.wikipedia.org/wiki/CURL";
   "https://en.wikipedia.org/wiki/OCaml"; "https://curl.haxx.se/"]

# open Lwt.Infix;;
# let codes =
    List.map (fun url -> Ezcurl_lwt.get ~url ()) urls
    |> Lwt_list.map_p
    (fun fut ->
      fut >>= function
      | Ok r -> Lwt.return r.Ezcurl_lwt.code
      | Error e -> Lwt.fail (Failure "oh no"))
  ;;
...
# codes;;
- : int list = [200; 200; 200]