Last active
April 27, 2021 14:37
-
-
Save patricoferris/caa45483b401ae626272cce7c7f59e98 to your computer and use it in GitHub Desktop.
Simple Cohttp Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let uri = "https://countries.trevorblades.com/" | |
let query = {| query { | |
countries { | |
name | |
} | |
}|} | |
let send_graphql_query query = | |
let open Lwt.Infix in | |
let headers = | |
Cohttp.Header.of_list [ "Content-type", "application/json"; "Accept", "application/json"] | |
in | |
let uri = Uri.of_string uri in | |
let body = | |
`O [("query", `String query)] | |
in | |
let serialized_body = Ezjsonm.to_string body in | |
Cohttp_lwt_unix.Client.post ~headers ~body:(`String serialized_body) uri >>= fun (_resp, body) -> | |
Cohttp_lwt.Body.to_string body >|= fun s -> | |
print_endline s | |
let () = | |
Lwt_main.run (send_graphql_query query) | |
(* | |
~~~ Dune File ~~~ | |
(executable | |
(name main) | |
(libraries cohttp-lwt-unix ezjsonm uri)) | |
Note: OCaml.org doesn't use dune, it will have to do it manually | |
via the Makefile, but I've added this in case you want to experiment | |
and also to show the dependencies. | |
To run: dune exec -- ./main.exe | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment