Created
December 5, 2011 06:28
-
-
Save zbroyar/1432555 to your computer and use it in GitHub Desktop.
OCaml CURL GET, POST, PUT, DELETE examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!