|
| 1 | +\page http HTTP Library |
| 2 | + |
| 3 | +```BASIC |
| 4 | +LIBRARY LIB$ + "/http" |
| 5 | +``` |
| 6 | + |
| 7 | +The HTTP library provides simple URL parsing helpers and minimal client-side HTTP request routines (HTTP/1.0 with `Connection: close`). |
| 8 | +Responses are stored in an internal buffer for later inspection. |
| 9 | + |
| 10 | +The following publicly documented procedures and functions are available via this library. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## URL helpers |
| 15 | + |
| 16 | +### FNhttp_url_scheme$(url$) |
| 17 | + |
| 18 | +Return the scheme (e.g. `http`, `https`, `ws`, `wss`, `ftp`) or an empty string if none. |
| 19 | + |
| 20 | +### FNhttp_url_anchor$(url$) |
| 21 | + |
| 22 | +Return the fragment (text after `#`) or an empty string if none. |
| 23 | + |
| 24 | +### FNhttp_url_query$(url$) |
| 25 | + |
| 26 | +Return the query string (text after `?`, excluding `#…`) or an empty string if none. |
| 27 | + |
| 28 | +### FNhttp_url_path$(url$) |
| 29 | + |
| 30 | +Return the path component beginning with `/`. Returns `/` for bare hosts and an empty string if not applicable. |
| 31 | + |
| 32 | +### FNhttp_url_host$(url$) |
| 33 | + |
| 34 | +Return the host portion of the authority. Supports IPv4/hostnames and bracketed IPv6. Returns an empty string if not present. |
| 35 | + |
| 36 | +### FNhttp_url_port(url$) |
| 37 | + |
| 38 | +Return the explicit port if present; otherwise the default for the scheme (`http`/`ws`=80, `https`/`wss`=443, `ftp`=21). |
| 39 | +Returns `0` if no scheme is recognised and no explicit port is present. |
| 40 | + |
| 41 | +### FNhttp_url_param$(url$, key$) |
| 42 | + |
| 43 | +Look up a single query parameter value by name. Returns the value if present, an empty string if the key appears with no value, or an empty string if the key is absent. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Requests |
| 48 | + |
| 49 | +### PROChttp_get(url$) |
| 50 | + |
| 51 | +Perform a `GET` request to `url$`. The full response (headers + body) is captured internally. |
| 52 | + |
| 53 | +### PROChttp_head(url$) |
| 54 | + |
| 55 | +Perform a `HEAD` request. Only headers are expected; body may be empty. |
| 56 | + |
| 57 | +### PROChttp_post(url$, postdata$) |
| 58 | + |
| 59 | +Perform a `POST` with `postdata$` as the raw request body. `Content-Length` is set automatically. |
| 60 | + |
| 61 | +### PROChttp_put(url$, putdata$) |
| 62 | + |
| 63 | +Perform a `PUT` with `putdata$` as the raw request body. |
| 64 | + |
| 65 | +### PROChttp_patch(url$, patchdata$) |
| 66 | + |
| 67 | +Perform a `PATCH` with `patchdata$` as the raw request body. |
| 68 | + |
| 69 | +### PROChttp_delete(url$) |
| 70 | + |
| 71 | +Perform a `DELETE` request. |
| 72 | + |
| 73 | +### PROChttp_request(method$, url$, body$) |
| 74 | + |
| 75 | +Low-level entry point used by the above helpers. Builds the request line and minimal headers (`Host`, `Connection: close`, optional `Content-Length`) and sends |
| 76 | +the request over HTTP, or HTTPS when the scheme/port implies HTTPS. The full response is stored for later retrieval. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Response access |
| 81 | + |
| 82 | +### FNhttp_result$(part$) |
| 83 | + |
| 84 | +Return a specific portion of the most recent response. `part$` must be one of: |
| 85 | + |
| 86 | +* `"body"` — response body only |
| 87 | +* `"headers"` — raw header block (status line + header lines, CRLF-delimited) |
| 88 | +* `"status"` — status line (e.g. `HTTP/1.1 200 OK`) |
| 89 | +* `"code"` — status code as a string (e.g. `200`) |
| 90 | +* `"reason"` — reason phrase (e.g. `OK`) |
| 91 | +* `"header:<name>"` — value of a single header (case-insensitive match on `<name>`). Returns the first occurrence, trimmed of surrounding whitespace. |
| 92 | + |
| 93 | +If no response is available, or the requested part does not exist, an empty string is returned. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Example |
| 98 | + |
| 99 | +```BASIC |
| 100 | +LIBRARY LIB$ + "/http" |
| 101 | +
|
| 102 | +PROChttp_get("http://neuron.brainbox.cc/test.txt") |
| 103 | +
|
| 104 | +PRINT "HTTP status code: "; FNhttp_result$("code") |
| 105 | +PRINT "Response body:" |
| 106 | +PRINT FNhttp_result$("body") |
| 107 | +``` |
| 108 | + |
| 109 | +This performs a simple `GET`, prints the numeric status code, then prints the body. |
0 commit comments