1 (** curl lwt example *)
8 let curl_setup_simple h
=
10 set_useragent h
"Curl_lwt";
12 set_connecttimeout h
5;
14 set_followlocation h
false;
16 set_encoding h CURL_ENCODING_ANY
20 let url = get_effectiveurl h
in
21 print_endline
@@ sprintf
"%3d %.2f %g URL: %s (%s)%s"
25 (if get_httpcode h
/ 100 = 3 then sprintf
"%s -> %s" url (get_redirecturl h
) else url)
27 (match code
with CURLE_OK
-> "" | _
-> sprintf
" %s (%d)" (strerror code
) (errno code
))
30 let b = Buffer.create
16 in
31 Curl.set_writefunction h
(fun s
-> Buffer.add_string
b s
; String.length s
);
32 Lwt.bind
(Curl_lwt.perform h
) (fun code
-> Lwt.return
(code
, Buffer.contents
b))
35 let h = Curl.init
() in
38 (* lwt (code,body) = download h in *)
39 Lwt.bind
(download h) @@ fun (code
,_body
) ->
41 (* do something with body *)
52 "forge.ocamlcore.org";
58 Lwt_main.run @@ run ()