Playground.Httpclaude: 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 = what the server answered: the URL it came from after the redirections, the status, the headers, the body's bytes
the body as text, when the status is 2xx (Bad_status otherwise)
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 post :
< Cap.network.. > ->
url:string ->
content_type:string ->
body:string ->
expect:'msg expect ->
'msg Cmd.ta 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 -> stringfor showing: "status 404", "network error: ... Connection refused"