What is the best module for HttpRequest in OCaml
Asked Answered
N

2

19

I wish to use OCaml to access the Yahoo Finance API. Essentially, it will be just a bunch of HTTP requests to get quotes from Yahoo Finance.

Which module I should use?

I wish to have async HTTP requests.

Nyx answered 3/1, 2013 at 6:34 Comment(7)
Did you consider Ocamlnet projects.camlcity.org/projects/ocamlnet.htmlVenous
@BasileStarynkevitch Not really, I am a total new learner and know nothing. Is Ocamlnet the best?Nyx
I don't know if it is the best, but it is very good.Venous
@BasileStarynkevitch does it provide async ability?Nyx
I've used to ocamlnet for this exact purpose recently there was no async ability.Underprivileged
rgrinberg, you are wrong, ocamlnet can issue async requestsChelsae
@BasileStarynkevitch could you please give me a simple sample code fragment for: loading a url, get the http page string out of it?Nyx
C
23

There are possibilities using lwt:

  • ocsigen has a quite complete and a bit complex implementation
  • cohttp is a bit simpler but lacks some usefull parts

using opam to install:

$ opam install ocsigenserver cohttp

For instance in a toplevel:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#thread;;
#require "ocsigenserver";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | { Ocsigen_http_frame.frame_content = Some v } ->
      Ocsigen_stream.string_of_stream 100000 (Ocsigen_stream.get v)
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Ocsigen_http_client.get_url
  [ "http://ocsigen.org/";
    "http://stackoverflow.com/" ]

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let result = Lwt_main.run t2

and using cohttp:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#require "cohttp.lwt";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | Some (_, body) -> Cohttp_lwt_unix.Body.string_of_body body
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
  (List.map Uri.of_string
     [ "http://example.org/";
       "http://example2.org/" ])

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let v = Lwt_main.run t2

Notice that an implementation of cohttp for jane street async library is also available

Chorography answered 3/1, 2013 at 10:8 Comment(7)
The "+../toplevel" is a bit misleading here: this is not necessary for compilers installed by OPAM and it won't work for the system compiler (you should use try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> () instead).Alcoholize
@Alcoholize I think your suggestion is very good. And I can't run the codes, but can with your try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> (). Could you please modify his other code?Monodrama
@Alcoholize I am a newbie, could you please tell me what is Some (_, body)?Nyx
It's a pattern matching against an optional pair. In this case, the first element of the pair is discarded and the second one is bind to the name "body". Read about "pattern-matching" if you want to know more about that.Alcoholize
@Alcoholize thanks for your reply. So Some is kind of keyword in pattern-matching?Nyx
@Alcoholize what is the meaning of t >>= in let t2 = t >>= Lwt_list.map_p content?Nyx
@JacksonTale It is some sugar for Lwt.bind. See Lwt docChorography
C
3

Just for the record, there is also ocurl with curl multi API support.

Chelsae answered 9/1, 2013 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.