public
Last active

OCaml CURL GET, POST, PUT, DELETE examples

  • Download Gist
gistfile1.ml
OCaml
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
(* ocamlfind ocamlopt -o exmpl -package curl -linkpkg exmpl.ml *)
open Printf
 
let _ = Curl.global_init Curl.CURLINIT_GLOBALALL
 
(*
*************************************************************************
** Aux. functions
*************************************************************************
*)
 
let writer_callback a d =
Buffer.add_string a d;
String.length d
 
let init_conn url =
let r = Buffer.create 16384
and c = Curl.init () in
Curl.set_timeout c 1200;
Curl.set_sslverifypeer c false;
Curl.set_sslverifyhost c Curl.SSLVERIFYHOST_EXISTENCE;
Curl.set_writefunction c (writer_callback r);
Curl.set_tcpnodelay c true;
Curl.set_verbose c false;
Curl.set_post c false;
Curl.set_url c url; r,c
 
 
(*
*************************************************************************
** GET, POST, PUT, DELETE
*************************************************************************
*)
 
 
(* GET *)
let get url =
let r,c = init_conn url in
Curl.set_followlocation c true;
Curl.perform c;
let rc = Curl.get_responsecode c in
Curl.cleanup c;
rc, (Buffer.contents r)
 
 
(* POST *)
let post ?(content_type = "text/html") url data =
let r,c = init_conn url in
Curl.set_post c true;
Curl.set_httpheader c [ "Content-Type: " ^ content_type ];
Curl.set_postfields c data;
Curl.set_postfieldsize c (String.length data);
Curl.perform c;
let rc = Curl.get_responsecode c in
Curl.cleanup c;
rc, (Buffer.contents r)
 
 
(* PUT *)
let put ?(content_type = "text/html") url data =
let pos = ref 0
and len = String.length data in
let rf cnt =
let can_send = len - !pos in
let to_send = if can_send > cnt then cnt else can_send in
let r = String.sub data !pos to_send in
pos := !pos + to_send; r
and r,c = init_conn url in
Curl.set_put c true;
Curl.set_upload c true;
Curl.set_readfunction c rf;
Curl.set_httpheader c [ "Content-Type: " ^ content_type ];
Curl.perform c;
let rc = Curl.get_responsecode c in
Curl.cleanup c;
rc, (Buffer.contents r)
 
 
(* DELETE *)
let delete url =
let r,c = init_conn url in
Curl.set_customrequest c "DELETE";
Curl.set_followlocation c false;
Curl.perform c;
let rc = Curl.get_responsecode c in
Curl.cleanup c;
rc, (Buffer.contents r)
 
(*
*************************************************************************
** Check
*************************************************************************
*)
 
 
let _ =
let r,c = put "http://localhost/test" "test" in
printf "%d -> %s\n" r c

Curl bindings looks too lowlevel, how about making a micro-library based on that gist?

Give me, please, more details about what would you like to see.

You've pretty much captured the concept in the gist, I think. Something similar to ocamlnet's Http_client.Convinience:

module HTTP : sig
  val delete : ~url:string -> response
  val get : ~url:string -> response
  val put : ?content_type:string -> ~url:string -> ~data:(string * string) list -> response
  val post : ?content_type:string -> ~url:string -> ~data:(string * string) list -> response
end

I'll put it in my todo :-)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.