Skip to content

Instantly share code, notes, and snippets.

@zbroyar
Created December 5, 2011 06:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zbroyar/1432555 to your computer and use it in GitHub Desktop.
Save zbroyar/1432555 to your computer and use it in GitHub Desktop.
OCaml CURL GET, POST, PUT, DELETE examples
(* 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
@superbobry
Copy link

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

@zbroyar
Copy link
Author

zbroyar commented Dec 5, 2011 via email

@superbobry
Copy link

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

@zbroyar
Copy link
Author

zbroyar commented Dec 5, 2011

I'll put it in my todo :-)

@superbobry
Copy link

Cool, thanks :)

@cljoly
Copy link

cljoly commented May 1, 2015

I started a library based on your code.

I plan to publish it under CeCILL-C. A quick explanation is available here.

@zbroyar Do you agree to publish it under CeCILL-C, as follow ?

(*************************************************************************************)
(* Copyright © zbroyar, 2011                                                         *)
(* Copyright © Joly Clément, 2015                                                    *)
(*                                                                                   *)
(* leowzukw@vmail.me                                                                 *)
(*                                                                                   *)
(* This software is a computer program whose purpose is to pass http requests.       *)
(*                                                                                   *)
(* This software is governed by the CeCILL-C license under French                    *)
(* law and abiding by the rules of distribution of free software.  You can  use,     *)
(* modify and/ or redistribute the software under the terms of the                   *)
(* CeCILL-C license as circulated by CEA, CNRS and INRIA at the                      *)
(* following URL "http://www.cecill.info".                                           *)
(*                                                                                   *)
(* As a counterpart to the access to the source code and  rights to copy, modify     *)
(* and redistribute granted by the license, users are provided only with a limited   *)
(* warranty  and the software's author,  the holder of the economic rights,  and     *)
(* the successive licensors  have only  limited liability.                           *)
(*                                                                                   *)
(* In this respect, the user's attention is drawn to the risks associated with       *)
(* loading,  using,  modifying and/or developing or reproducing the software by the  *)
(* user in light of its specific status of free software, that may mean  that it is  *)
(* complicated to manipulate,  and  that  also therefore means  that it is reserved  *)
(* for developers  and  experienced professionals having in-depth computer           *)
(* knowledge. Users are therefore encouraged to load and test the software's         *)
(* suitability as regards their requirements in conditions enabling the security of  *)
(* their systems and/or data to be ensured and,  more generally, to use and operate  *)
(* it in the same conditions as regards security.                                    *)
(*                                                                                   *)
(* The fact that you are presently reading this means that you have had knowledge    *)
(* of the CeCILL-C license and that you accept its terms.                            *)
(*                                                                                   *)
(*************************************************************************************)

@zbroyar
Copy link
Author

zbroyar commented May 23, 2015

Hi Leo,

Please, feel yourself free to do with the code whatever you want. It's public domain :-)

@cljoly
Copy link

cljoly commented Jun 17, 2015

Thanks!

@cljoly
Copy link

cljoly commented Jun 20, 2015

You can find a repository on Gitlab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment