An open API service indexing awesome lists of open source software.

https://github.com/hcarty/ezrest

Easy REST requests using cohttp
https://github.com/hcarty/ezrest

Last synced: 9 months ago
JSON representation

Easy REST requests using cohttp

Awesome Lists containing this project

README

          

# Easy REST requests using cohttp

See `ezrest.mli` for detailed usage information.

## Example usage
Let's take a look at what it's like to use Ezrest from a REPL.
```ocaml require-package=ezrest env=demo
# #require "ezrest"
```

First, a simple `HEAD` request.
```ocaml env=demo
# let site = Uri.of_string "http://jsonplaceholder.typicode.com/"
val site : Uri.t =
# Ezrest.head site
- : Cohttp.Response.t Ezrest.result =
Ok
{Cohttp.Response.encoding = Cohttp__.Transfer.Unknown; headers = ;
version = `HTTP_1_1; status = `OK; flush = false}
```

Now we can `GET` some content, failing if the site tries to redirect us.
```ocaml env=demo
# Ezrest.get ~follow:0 site
- : string Ezrest.result =
Ok
"\n\n\n\n\n\n\n
# Ezrest.put put_site
- : string Ezrest.result = Ok "{\n \"id\": 1\n}"
# let post_site = Uri.with_path site "/posts"
val post_site : Uri.t =
# Ezrest.post post_site
- : string Ezrest.result = Ok "{\n \"id\": 101\n}"
```

And `DELETE` if that's what's necessary.
```ocaml env=demo
# Ezrest.delete put_site
- : string Ezrest.result = Ok "{}"
```