|
1 | 1 | # Response
|
| 2 | + |
| 3 | +In AIScript, the `Response` object provides a flexible way to control HTTP responses from your routes. This object gives you complete control over status codes, response bodies, headers, and cookies. |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +The most basic way to create a response is to simply return a value from a route: |
| 8 | + |
| 9 | +```js |
| 10 | +get /hello { |
| 11 | + return "Hello, World!"; |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +AIScript automatically converts this to a proper HTTP response with status code 200 and the appropriate content type. |
| 16 | + |
| 17 | +## Creating Custom Responses |
| 18 | + |
| 19 | +For more control, use the `Response` constructor: |
| 20 | + |
| 21 | +```js |
| 22 | +get /api/user { |
| 23 | + return response( |
| 24 | + status_code=200, |
| 25 | + body={"id": 1, "name": "Alice"}, |
| 26 | + headers={"X-API-Version": "1.0"}, |
| 27 | + cookies={"session": "abc123"} |
| 28 | + ); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The `Response` constructor accepts the following parameters: |
| 33 | + |
| 34 | +| Parameter | Type | Description | Default | |
| 35 | +|-----------|------|-------------|---------| |
| 36 | +| `status_code` | Number | HTTP status code (100-599) | 200 | |
| 37 | +| `body` | Any | Response body (can be string, object, array, etc.) | nil | |
| 38 | +| `headers` | Object | HTTP headers as key-value pairs | {} | |
| 39 | +| `cookies` | Object | Cookies as key-value pairs | {} | |
| 40 | + |
| 41 | +## Status Codes |
| 42 | + |
| 43 | +You can set any valid HTTP status code: |
| 44 | + |
| 45 | +```js |
| 46 | +get /not-found { |
| 47 | + return response( |
| 48 | + status_code=404, |
| 49 | + body="Resource not found" |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +get /error { |
| 54 | + return response( |
| 55 | + status_code=500, |
| 56 | + body="Internal server error" |
| 57 | + ); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Response Body |
| 62 | + |
| 63 | +The response body can be of any type: |
| 64 | + |
| 65 | +```js |
| 66 | +// String response |
| 67 | +get /text { |
| 68 | + return response(body="Plain text response"); |
| 69 | +} |
| 70 | + |
| 71 | +// JSON response (object automatically serialized to JSON) |
| 72 | +get /json { |
| 73 | + return response(body={ |
| 74 | + "id": 123, |
| 75 | + "name": "Product", |
| 76 | + "price": 29.99 |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +// Array response |
| 81 | +get /array { |
| 82 | + return response(body=[1, 2, 3, 4, 5]); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Headers |
| 87 | + |
| 88 | +Set custom HTTP headers: |
| 89 | + |
| 90 | +```js |
| 91 | +get /api/data { |
| 92 | + return response( |
| 93 | + body={"data": "value"}, |
| 94 | + headers={ |
| 95 | + "Content-Type": "application/json", |
| 96 | + "X-API-Version": "2.0", |
| 97 | + "Cache-Control": "max-age=3600" |
| 98 | + } |
| 99 | + ); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Cookies |
| 104 | + |
| 105 | +Set cookies in the response: |
| 106 | + |
| 107 | +```js |
| 108 | +get /login { |
| 109 | + return response( |
| 110 | + body="Login successful", |
| 111 | + cookies={ |
| 112 | + "session": "abc123", |
| 113 | + "user": "john", |
| 114 | + "preferences": "dark-mode" |
| 115 | + } |
| 116 | + ); |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Redirects |
| 121 | + |
| 122 | +AIScript provides convenient functions for creating redirect responses: |
| 123 | + |
| 124 | +### Temporary Redirect (307) |
| 125 | + |
| 126 | +```js |
| 127 | +get /old-page { |
| 128 | + return temporary_redirect(target="/new-page"); |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Permanent Redirect (308) |
| 133 | + |
| 134 | +```js |
| 135 | +get /outdated { |
| 136 | + return permanent_redirect(target="/updated"); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Advanced Examples |
| 141 | + |
| 142 | +### Conditional Response |
| 143 | + |
| 144 | +```js |
| 145 | +get /api/users/<id:int> { |
| 146 | + use std.db.pg; |
| 147 | + |
| 148 | + let user = pg.query_one("SELECT * FROM users WHERE id = $1", id); |
| 149 | + |
| 150 | + if user == nil { |
| 151 | + return response( |
| 152 | + status_code=404, |
| 153 | + body={"error": "User not found"} |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + return response( |
| 158 | + status_code=200, |
| 159 | + body=user, |
| 160 | + headers={"X-User-Id": str(id)} |
| 161 | + ); |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### File Download |
| 166 | + |
| 167 | +```js |
| 168 | +get /download { |
| 169 | + use std.io; |
| 170 | + |
| 171 | + let file_content = io.read_file("path/to/document.pdf"); |
| 172 | + |
| 173 | + return response( |
| 174 | + body=file_content, |
| 175 | + headers={ |
| 176 | + "Content-Type": "application/pdf", |
| 177 | + "Content-Disposition": "attachment; filename=\"document.pdf\"" |
| 178 | + } |
| 179 | + ); |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### CORS Support |
| 184 | + |
| 185 | +```js |
| 186 | +get /api/data { |
| 187 | + return response( |
| 188 | + body={"message": "API data"}, |
| 189 | + headers={ |
| 190 | + "Access-Control-Allow-Origin": "*", |
| 191 | + "Access-Control-Allow-Methods": "GET, POST, OPTIONS", |
| 192 | + "Access-Control-Allow-Headers": "Content-Type, Authorization" |
| 193 | + } |
| 194 | + ); |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +## Error Responses |
| 199 | + |
| 200 | +AIScript automatically generates appropriate error responses for validation failures and exceptions, but you can also create custom error responses: |
| 201 | + |
| 202 | +```js |
| 203 | +get /api/resource { |
| 204 | + if !authorized() { |
| 205 | + return response( |
| 206 | + status_code=401, |
| 207 | + body={"error": "Unauthorized access"} |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + if !resource_exists() { |
| 212 | + return response( |
| 213 | + status_code=404, |
| 214 | + body={"error": "Resource not found"} |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + // Continue with normal processing... |
| 219 | +} |
| 220 | +``` |
0 commit comments