Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Last active April 27, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patricoferris/caa45483b401ae626272cce7c7f59e98 to your computer and use it in GitHub Desktop.
Save patricoferris/caa45483b401ae626272cce7c7f59e98 to your computer and use it in GitHub Desktop.
Simple Cohttp Post
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