site/contents/reference-http.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
-----
content-type: "page"
title: "http Module"
-----
{@ _defs_.md || 0 @}
{#op||download||{{s1}} {{s2}}||{{null}}||
Downloads the contents of URL {{s1}} to the local file {{s2}}. #}
{#op||get-content||{{s1}}||{{s2}}||
Retrieves the contents of URL {{s1}} as {{s2}}.#}
{#op||request||{{req}}||{{res}}||
> Performs an HTTP request.
>
> > %sidebar%
> > Example
> >
> > The following code constructs a {{req}} dictionary using the **tap** operator and passes it to the **request** operator to perform an HTTP GET request to <http://httpbin.org/ip>:
> >
> > {} (
> > ("GET" %method)
> > ("http://httpbin.org/ip" %url)
> > ) tap request
#}
{#op||start-server||{{d}}||{{null}}||
> Starts an HTTP server based on the configuration provided in {{d}}.
>
> {{d}} is a dictionary containing the following keys:
>
> address
> : The address to bind the server to (default: **127.0.0.1**).
> port
> : The port to bind the server to.
> handler
> : A quotation with the following signature, used to handle all incoming requests: [{{req}} ⇒ {{res}}](class:kwd)
>
> > %sidebar%
> > Example
> >
> > The following program starts a very simple HTTP server that can display the current timestamp or date and time in ISO 8601 format:
> >
> > (
> > :req
> > req /url :url
> > ;Set response body
> > "Invalid Request: $1" (url) => % :body
> > ("/datetime" url ==) (
> > timestamp datetime @body
> > ) when
> > ("/timestamp" url ==) (
> > timestamp string @body
> > ) when
> > ("/shutdown" url ==) (
> > "Stopping server..." puts!
> > stop-server
> > ) when
> > ;Prepare response
> > {} body %body
> > ) =handler
> >
> > ;Start server
> > "Server started on port 5555." puts!
> > "Press Ctrl+C to stop." puts!
> >
> > {}
> > handler %handler
> > 5555 %port
> > start-server
#}
{#op||stop-server||{{null}}||{{null}}||
Stops the currently-running HTTP server. This operator should be used within an HTTP server handler quotation.#}
|