Response Namespace
The Response namespace is a field in Request instance that contains the payload and information Jarvis uses to format and send a response.
Every Request has its own Response namespace, req.Response. You can set its fields directly, or use the Request methods SetStatus, Fail, SetHeader, AddHeader, DefaultHeader and SetContentType, which update Response for you.
In many cases you won't need to touch Response at all; Jarvis builds the response from your endpoint's result.
How Jarvis builds the response
Once your endpoint (or REST handler) returns, Jarvis:
- Uses the endpoint's result as
Payload, but only if you haven't already setPayloadyourself. - Calls
PostProcessFn, if you've defined one. - Sets the
Content-Typeheader toDefaultContentTypeif it hasn't been set. - Converts
Payloadto JSON if the content-type isapplication/json. - Compresses
PayloadifUseZipis enabled, the payload is large enough, and the client accepts compressed content. - Adds
ServerandDateheaders and sends the response.
In the REST paradigm, steps 3 and 4 are skipped for a non-2xx response unless RESTFailProcessing is 1.
Response Fields
Status
| Description | Status is the HTTP status code to send to the client. |
| Default | 0 |
| Notes |
|
StatusText
| Description | StatusText is the reason phrase sent with Status, for example 'Not Found'. |
| Default | 'OK' |
| Notes | SetStatus and Fail look up the standard reason phrase for the status code and append any message you supply in parentheses. If you assign Status directly, remember to set StatusText to match. |
Payload
| Description | Payload is the body of the response. |
| Default | '' |
| Notes |
|
| Examples | req.Response.Payload←'<h1>Hello</h1>'req.Response.Payload←'' '/var/www/report.pdf' |
Headers
| Description | Headers is a 2-column matrix of the response's [;1] header names and [;2] header values. |
| Default | 0 2⍴'' '' |
| Notes |
|
Examples
A REST handler that returns 201 Created with a Location header:
∇ r←Post req;id
[1] id←SaveItem req.Payload
[2] req.SetStatus 201
[3] 'Location'req.SetHeader req.MakeURI id
[4] r←⎕NS''
[5] r.id←id
∇
A JSON endpoint that returns plain text instead of JSON:
∇ r←req Version dummy
[1] req.SetContentType'text/plain; charset=utf-8'
[2] r←'Version 1.2.3'
∇
An endpoint that fails the request with a message:
∇ r←req GetItem args
[1] r←''
[2] →0 If'Item not found'req.Fail 404×~ItemExists args.id
[3] r←LoadItem args.id
∇