Module Playground.Http

claude: Elm's Http, asking a server for something from init or update: a command (Cmd.t) the platform performs, the answer coming back as a message, without the frames stopping meanwhile.

    type msg = GotText of (string, Http.error) result

    let init caps _flags =
      (Loading, Http.get caps ~url:"http://localhost:8001/examples/HttpText.ml"
                  ~expect:(Http.expect_string (fun result -> GotText result)))

    let update msg _model =
      match msg with
      | GotText (Ok text) -> (Success text, Cmd.none)
      | GotText (Error e) -> (Failure (Http.error_to_string e), Cmd.none)

Natively, http:// by our own client, stepped every frame, and https:// by curl, blocking (the frame waits), until TLS is written; in a browser, whatever the browser allows (the page's own server, or another that says so: CORS). Only for the app level: picture, animation and game have no commands, as in Evan's playground. It takes the capability to reach the network (< Cap.network; .. >: any capabilities that include it, from the program's Cap.main), which the command carries. See examples/HttpText.ml.

type error = Cmd.http_error =
  1. | Bad_url of string
  2. | Timeout
  3. | Network_error of string
  4. | Bad_status of int
  5. | Bad_body of string
type 'msg expect

what to do with the answer: Elm's Http.Expect

type response = Cmd.http_response = {
  1. url : string;
  2. status : int;
  3. headers : (string * string) list;
  4. body : string;
}

what the server answered: the URL it came from after the redirections, the status, the headers, the body's bytes

val expect_string : ((string, error) Stdlib.result -> 'msg) -> 'msg expect

the body as text, when the status is 2xx (Bad_status otherwise)

val expect_response : ((response, error) Stdlib.result -> 'msg) -> 'msg expect

the whole response, whatever its status (Elm's expectBytesResponse): what a browser needs -- a 404's page is shown too, an image's bytes are not text, and the page's links are relative to the URL it finally came from (TinyMosaic)

val get : < Cap.network.. > -> url:string -> expect:'msg expect -> 'msg Cmd.t
val post : < Cap.network.. > -> url:string -> content_type:string -> body:string -> expect:'msg expect -> 'msg Cmd.t

a POST of body, of content_type (a form's fields: "application/x-www-form-urlencoded", TinyMosaic); a redirection answered with a GET, as browsers do

val error_to_string : error -> string

for showing: "status 404", "network error: ... Connection refused"