HttpResponse

Inherits from: Object

An HTTP response returned from an HttpServer route handler.

HttpResponse wraps a status code, body string, and headers map. Create responses with new:body: and optionally set headers before returning from a route handler block.

Example
"Simple 200 response"
HttpResponse new: 200 body: 'Hello, world!'
Example
"JSON response with content type"
| response <HttpResponse> |
response := HttpResponse new: 200 body: '{"status": "ok"}'.
response contentType: 'application/json'
Example
"Response with custom headers"
| response <HttpResponse> |
response := HttpResponse new: 201 body: 'Created'.
response header: 'Location' value: '/api/items/42'.
response contentType: 'application/json'

Class Methods

primitives

class new:body:

Create a new HTTP response with the given status code and body string.

Example
HttpResponse new: 200 body: 'OK'
HttpResponse new: 404 body: 'Not Found'
HttpResponse new: 500 body: 'Internal Server Error'

Instance Methods

primitives

body

Return the body string of this response.

Example
(HttpResponse new: 200 body: 'hello') body   "=> 'hello'"
contentType:

Set the Content-Type header. Convenience method equivalent to self header: 'Content-Type' value: mime. Returns self for chaining.

Example
response contentType: 'application/json'
header:value:

Set a response header. Returns self for chaining.

Example
response header: 'X-Custom' value: 'my-value'
status

Return the status code of this response.

Example
(HttpResponse new: 200 body: 'OK') status   "=> 200"