Skip to content

Instantly share code, notes, and snippets.

@Leonidas-from-XIV
Last active March 5, 2019 22:11
Show Gist options
  • Save Leonidas-from-XIV/fb7739facfae5af9d8215a1b4d413818 to your computer and use it in GitHub Desktop.
Save Leonidas-from-XIV/fb7739facfae5af9d8215a1b4d413818 to your computer and use it in GitHub Desktop.
A variant of https://pastebin.com/FNC2PMQP that works
(executable
(name FNC2PMQP)
(public_name FNC2PMQP)
(libraries cohttp cohttp-lwt cohttp-lwt-unix lwt))
open Lwt.Infix
open Cohttp
open Cohttp_lwt_unix
let body () =
Client.get (Uri.of_string "https://hacker-news.firebaseio.com/v0/maxitem.json") >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
Lwt_io.printf "Response Code: %d\n" code >>= fun () ->
Lwt_io.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string) >>= fun () ->
body |> Cohttp_lwt.Body.to_string >>= fun body ->
Lwt_io.printf "Body of length: %d\n" (String.length body) >|= fun () ->
body
let getItem item =
Client.get (Uri.of_string (String.concat "" ["https://hacker-news.firebaseio.com/v0/item/"; item; ".json?print=pretty"])) >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
Lwt_io.printf "Response Code: %d\n" code >>= fun () ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
body
let main () =
body ()
>>= getItem
>>= Lwt_io.printf "Result: %s"
let () =
Lwt_main.run @@ main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment