File: cohttp_curl_async.ml

package info (click to toggle)
ocaml-cohttp 5.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,088 kB
  • sloc: ml: 7,793; javascript: 15; makefile: 12
file content (52 lines) | stat: -rw-r--r-- 1,897 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(*{{{ Copyright (c) 2014 Anil Madhavapeddy <anil@recoil.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
  }}}*)

open Base
open Async_kernel
open Cohttp_async

let show_headers h =
  Cohttp.Header.iter (fun k v -> Logs.info (fun m -> m "%s: %s%!" k v)) h

let make_net_req uri meth' body () =
  let meth = Cohttp.Code.method_of_string meth' in
  let uri = Uri.of_string uri in
  let headers = Cohttp.Header.of_list [ ("connection", "close") ] in
  Client.call meth ~headers ~body:Body.(of_string body) uri
  >>= fun (res, body) ->
  show_headers (Cohttp.Response.headers res);
  body
  |> Body.to_pipe
  |> Pipe.iter ~f:(fun b ->
         Stdlib.print_string b;
         return ())

let _ =
  (* enable logging to stdout *)
  Fmt_tty.setup_std_outputs ();
  Logs.set_level @@ Some Logs.Debug;
  Logs.set_reporter (Logs_fmt.reporter ());
  let open Async_command in
  async_spec ~summary:"Fetch URL and print it"
    Spec.(
      empty
      +> anon ("url" %: string)
      +> flag "-X" (optional_with_default "GET" string) ~doc:" Set HTTP method"
      +> flag "data-binary"
           (optional_with_default "" string)
           ~doc:" Data to send when using POST")
    make_net_req
  |> Command_unix.run