|
1 | 1 | @page http_module HTTP module |
2 | 2 |
|
3 | | -A module to play with HTTP requests and create web servers, using [cpp-httplib](https://github.com/yhirose/cpp-httplib) (MIT License). |
4 | | - |
5 | | -* @subpage http_server |
6 | | -* @subpage http_client |
| 3 | +A module to play with HTTP requests, using [cpp-httplib](https://github.com/yhirose/cpp-httplib) (MIT License). |
7 | 4 |
|
8 | 5 | Disclaimer: this module does not handle |
9 | 6 | * multipart/form-data POST requests |
10 | 7 | * content received with content receiver |
11 | 8 | * content sent with content receiver |
12 | 9 | * chunked transfer encoding |
13 | | -* server-sent events |
14 | | -* multiple servers per program |
15 | 10 | * digest authentication (needs OpenSSL and libcrypto for that) |
| 11 | + |
| 12 | +## http:headers |
| 13 | + |
| 14 | +Create a header map to use with the http client. |
| 15 | + |
| 16 | +**Parameters** |
| 17 | +They work as pairs with: |
| 18 | +- `name`: string |
| 19 | +- `value`: string |
| 20 | + |
| 21 | +**Return value** `UserType<httpHeaders>` |
| 22 | + |
| 23 | +**Author** |
| 24 | +- [@SuperFola](https://github.com/SuperFola) |
| 25 | + |
| 26 | +**Example** |
| 27 | +~~~~{.lisp} |
| 28 | +(let headers (http:headers |
| 29 | + # pair 0 |
| 30 | + "Accept-Encoding" "gzip, deflate" |
| 31 | + # pair 1 |
| 32 | + "Content-Type" "application/json")) |
| 33 | +~~~~ |
| 34 | + |
| 35 | +## http:client |
| 36 | + |
| 37 | +Create a http client to query a server. |
| 38 | + |
| 39 | +**Parameters** |
| 40 | +- `host`: string, you must not put the protocol `http://` nor `https://` |
| 41 | +- `port`: number |
| 42 | + |
| 43 | +**Return value** `List` if the request succeeded: `[status, body]`, otherwise `nil` |
| 44 | + |
| 45 | +**Author** |
| 46 | +- [@SuperFola](https://github.com/SuperFola) |
| 47 | + |
| 48 | +**Example** |
| 49 | +~~~~{.lisp} |
| 50 | +(let cli (http:client "localhost" 1234)) |
| 51 | +~~~~ |
| 52 | + |
| 53 | +## http:get |
| 54 | + |
| 55 | +Get content from an online resource. |
| 56 | + |
| 57 | +**Parameters** |
| 58 | +- `client`: `UserType<httpClient>` |
| 59 | +- `route`: string |
| 60 | +- `headers`: `UserType<httpHeaders>`, optional, always come last |
| 61 | + |
| 62 | +**Return value** `List` if the request succeeded: `[status, body]`, otherwise `nil` |
| 63 | + |
| 64 | +**Author** |
| 65 | +- [@SuperFola](https://github.com/SuperFola) |
| 66 | + |
| 67 | +**Example** |
| 68 | +~~~~{.lisp} |
| 69 | +(let headers (http:headers:create |
| 70 | + "Accept-Encoding" "gzip, deflate")) |
| 71 | +(let cli (http:client "localhost" 1234)) |
| 72 | +
|
| 73 | +(mut output (http:get cli "/hi" headers)) |
| 74 | +(if (nil? output) |
| 75 | + (print "couldn't reach the server") |
| 76 | + (print (@ output 0))) # print the status |
| 77 | +~~~~ |
| 78 | + |
| 79 | +## http:post, http:put, http:patch, http:delete |
| 80 | + |
| 81 | +Make a POST / PUT / PATCH / DELETE request with a String as the request's body. |
| 82 | + |
| 83 | +**Parameters** |
| 84 | +- `client`: `UserType<httpClient>` |
| 85 | +- `route`: string |
| 86 | +- `body`: string (request body) |
| 87 | +- `mimetype`: string, optional (defaults to `text/plain`) |
| 88 | +- `headers`: `UserType<httpHeaders>`, optional, always come last |
| 89 | + |
| 90 | +**Return value** `List` if the request succeeded: `[status, body]`, otherwise `nil` |
| 91 | + |
| 92 | +**Author** |
| 93 | +- [@SuperFola](https://github.com/SuperFola) |
| 94 | + |
| 95 | +**Example** |
| 96 | +~~~~{.lisp} |
| 97 | +(let cli (http:client "localhost" 1234)) |
| 98 | +
|
| 99 | +(mut output (http:post cli "/hi" "{\"key\": 5}" "application/json" headers)) |
| 100 | +(if (nil? output) |
| 101 | + (print "couldn't reach the server") |
| 102 | + (print (@ output 0))) # print the status |
| 103 | +~~~~ |
0 commit comments