|
1 | 1 | # std.http
|
| 2 | + |
| 3 | +The `std.http` module provides functions for making HTTP requests to external services and APIs. |
| 4 | + |
| 5 | +## get(url: string, headers?: object) |
| 6 | + |
| 7 | +Makes an HTTP GET request to the specified URL. |
| 8 | + |
| 9 | +**Parameters**: |
| 10 | +- `url`: The URL to send the request to |
| 11 | +- `headers` (optional): An object containing HTTP headers |
| 12 | + |
| 13 | +**Return Type**: `object` with the following properties: |
| 14 | +- `status`: HTTP status code (number) |
| 15 | +- `statusText`: HTTP status message (string) |
| 16 | +- `ok`: Whether the request was successful (boolean) |
| 17 | +- `headers`: Response headers (object) |
| 18 | +- `text`: Response body as text (string) |
| 19 | +- `json`: Response body parsed as JSON (if applicable) |
| 20 | + |
| 21 | +**Example**: |
| 22 | +```rust |
| 23 | +use std.http; |
| 24 | + |
| 25 | +// Simple GET request |
| 26 | +let response = http.get("https://api.example.com/users"); |
| 27 | +if (response.ok) { |
| 28 | + println(response.json); |
| 29 | +} else { |
| 30 | + println(`Error: ${response.status} ${response.statusText}`); |
| 31 | +} |
| 32 | + |
| 33 | +// GET request with headers |
| 34 | +let response = http.get("https://api.example.com/users", { |
| 35 | + "Authorization": "Bearer token123", |
| 36 | + "Accept": "application/json" |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +## post(url: string, body: string, headers?: object) |
| 41 | + |
| 42 | +Makes an HTTP POST request to the specified URL with the given body. |
| 43 | + |
| 44 | +**Parameters**: |
| 45 | +- `url`: The URL to send the request to |
| 46 | +- `body`: The request body as a string |
| 47 | +- `headers` (optional): An object containing HTTP headers |
| 48 | + |
| 49 | +**Return Type**: `object` (same as get) |
| 50 | + |
| 51 | +**Example**: |
| 52 | +```rust |
| 53 | +use std.http; |
| 54 | + |
| 55 | +// POST request with JSON body |
| 56 | +let response = http.post( |
| 57 | + "https://api.example.com/users", |
| 58 | + '{"name": "John", "email": "john@example.com"}', |
| 59 | + { |
| 60 | + "Content-Type": "application/json", |
| 61 | + "Authorization": "Bearer token123" |
| 62 | + } |
| 63 | +); |
| 64 | + |
| 65 | +if (response.ok) { |
| 66 | + println(`User created with ID: ${response.json.id}`); |
| 67 | +} else { |
| 68 | + println(`Error: ${response.status} ${response.statusText}`); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## put(url: string, body: string, headers?: object) |
| 73 | + |
| 74 | +Makes an HTTP PUT request to the specified URL with the given body. |
| 75 | + |
| 76 | +**Parameters**: |
| 77 | +- `url`: The URL to send the request to |
| 78 | +- `body`: The request body as a string |
| 79 | +- `headers` (optional): An object containing HTTP headers |
| 80 | + |
| 81 | +**Return Type**: `object` (same as get) |
| 82 | + |
| 83 | +**Example**: |
| 84 | +```rust |
| 85 | +use std.http; |
| 86 | + |
| 87 | +// PUT request to update a resource |
| 88 | +let response = http.put( |
| 89 | + "https://api.example.com/users/123", |
| 90 | + '{"name": "John Updated", "email": "john.updated@example.com"}', |
| 91 | + { |
| 92 | + "Content-Type": "application/json", |
| 93 | + "Authorization": "Bearer token123" |
| 94 | + } |
| 95 | +); |
| 96 | + |
| 97 | +if (response.ok) { |
| 98 | + println("User updated successfully"); |
| 99 | +} else { |
| 100 | + println(`Error: ${response.status} ${response.statusText}`); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## delete(url: string, headers?: object) |
| 105 | + |
| 106 | +Makes an HTTP DELETE request to the specified URL. |
| 107 | + |
| 108 | +**Parameters**: |
| 109 | +- `url`: The URL to send the request to |
| 110 | +- `headers` (optional): An object containing HTTP headers |
| 111 | + |
| 112 | +**Return Type**: `object` (same as get) |
| 113 | + |
| 114 | +**Example**: |
| 115 | +```rust |
| 116 | +use std.http; |
| 117 | + |
| 118 | +// DELETE request to remove a resource |
| 119 | +let response = http.delete( |
| 120 | + "https://api.example.com/users/123", |
| 121 | + { |
| 122 | + "Authorization": "Bearer token123" |
| 123 | + } |
| 124 | +); |
| 125 | + |
| 126 | +if (response.ok) { |
| 127 | + println("User deleted successfully"); |
| 128 | +} else { |
| 129 | + println(`Error: ${response.status} ${response.statusText}`); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## patch(url: string, body: string, headers?: object) |
| 134 | + |
| 135 | +Makes an HTTP PATCH request to the specified URL with the given body. |
| 136 | + |
| 137 | +**Parameters**: |
| 138 | +- `url`: The URL to send the request to |
| 139 | +- `body`: The request body as a string |
| 140 | +- `headers` (optional): An object containing HTTP headers |
| 141 | + |
| 142 | +**Return Type**: `object` (same as get) |
| 143 | + |
| 144 | +**Example**: |
| 145 | +```rust |
| 146 | +use std.http; |
| 147 | + |
| 148 | +// PATCH request to partially update a resource |
| 149 | +let response = http.patch( |
| 150 | + "https://api.example.com/users/123", |
| 151 | + '{"email": "new.email@example.com"}', |
| 152 | + { |
| 153 | + "Content-Type": "application/json", |
| 154 | + "Authorization": "Bearer token123" |
| 155 | + } |
| 156 | +); |
| 157 | + |
| 158 | +if (response.ok) { |
| 159 | + println("User partially updated successfully"); |
| 160 | +} else { |
| 161 | + println(`Error: ${response.status} ${response.statusText}`); |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +## head(url: string, headers?: object) |
| 166 | + |
| 167 | +Makes an HTTP HEAD request to the specified URL. |
| 168 | + |
| 169 | +**Parameters**: |
| 170 | +- `url`: The URL to send the request to |
| 171 | +- `headers` (optional): An object containing HTTP headers |
| 172 | + |
| 173 | +**Return Type**: `object` (same as get, but typically without body content) |
| 174 | + |
| 175 | +**Example**: |
| 176 | +```rust |
| 177 | +use std.http; |
| 178 | + |
| 179 | +// HEAD request to check if a resource exists |
| 180 | +let response = http.head("https://api.example.com/users/123"); |
| 181 | + |
| 182 | +if (response.ok) { |
| 183 | + println("Resource exists"); |
| 184 | + println(`Last-Modified: ${response.headers["last-modified"]}`); |
| 185 | +} else { |
| 186 | + println("Resource not found"); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Usage Examples |
| 191 | + |
| 192 | +## Fetching JSON Data |
| 193 | +```rust |
| 194 | +use std.http; |
| 195 | + |
| 196 | +// Get JSON data from an API |
| 197 | +let response = http.get("https://api.example.com/data"); |
| 198 | +if (response.ok) { |
| 199 | + // Access parsed JSON directly |
| 200 | + let items = response.json.items; |
| 201 | + for (item in items) { |
| 202 | + println(`${item.id}: ${item.name}`); |
| 203 | + } |
| 204 | +} else { |
| 205 | + println(`Failed to fetch data: ${response.status}`); |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +## Handling Authentication |
| 210 | +```rust |
| 211 | +use std.http; |
| 212 | + |
| 213 | +// Function to make authenticated requests |
| 214 | +fn authenticated_request(url, method, body = nil) { |
| 215 | + let token = "your-auth-token"; |
| 216 | + let headers = { |
| 217 | + "Authorization": `Bearer ${token}`, |
| 218 | + "Content-Type": "application/json" |
| 219 | + }; |
| 220 | + |
| 221 | + if (method == "GET") { |
| 222 | + return http.get(url, headers); |
| 223 | + } else if (method == "POST") { |
| 224 | + return http.post(url, body, headers); |
| 225 | + } else if (method == "PUT") { |
| 226 | + return http.put(url, body, headers); |
| 227 | + } else if (method == "DELETE") { |
| 228 | + return http.delete(url, headers); |
| 229 | + } |
| 230 | + |
| 231 | + return nil; |
| 232 | +} |
| 233 | + |
| 234 | +// Use the function |
| 235 | +let user_data = authenticated_request("https://api.example.com/me", "GET"); |
| 236 | +println(user_data.json); |
| 237 | +``` |
| 238 | + |
| 239 | +## Submitting Form Data |
| 240 | +```rust |
| 241 | +use std.http; |
| 242 | + |
| 243 | +// Submit form data |
| 244 | +let form_data = 'name=John&email=john@example.com&message=Hello'; |
| 245 | +let response = http.post( |
| 246 | + "https://example.com/contact", |
| 247 | + form_data, |
| 248 | + { |
| 249 | + "Content-Type": "application/x-www-form-urlencoded" |
| 250 | + } |
| 251 | +); |
| 252 | +
|
| 253 | +if (response.ok) { |
| 254 | + println("Form submitted successfully"); |
| 255 | +} else { |
| 256 | + println(`Form submission failed: ${response.text}`); |
| 257 | +} |
| 258 | +``` |
0 commit comments