Skip to content

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:

  1. Uses the endpoint's result as Payload, but only if you haven't already set Payload yourself.
  2. Calls PostProcessFn, if you've defined one.
  3. Sets the Content-Type header to DefaultContentType if it hasn't been set.
  4. Converts Payload to JSON if the content-type is application/json.
  5. Compresses Payload if UseZip is enabled, the payload is large enough, and the client accepts compressed content.
  6. Adds Server and Date headers 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
  • If ValidateRequestFn accepts the request and Status is still 0, Jarvis sets it to 200 before calling your endpoint. If ValidateRequestFn rejects the request and doesn't set a status, Jarvis uses 400.
  • Use SetStatus or Fail rather than assigning Status directly. They also set StatusText.
  • In the JSON paradigm, if your endpoint returns no result, leaves Payload empty and leaves Status at 200, Jarvis responds with 204 No Content.
  • Jarvis closes the connection after sending any non-2xx response.

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
  • If you set Payload in your endpoint, Jarvis sends it in preference to your endpoint's result.
  • If the response content-type is application/json, Payload should be an APL array; Jarvis converts it to JSON. Otherwise, Payload should be a character vector or a vector of byte values.
  • To send a file, set Payload to a 2-element vector of an empty vector and the file name, ''filename. Conga reads and sends the file. Use ContentTypeForFile to set an appropriate content-type.
  • If HTMLInterface is enabled and a request fails with an empty Payload, Jarvis sends a short HTML page showing the status and status text.
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
  • Use SetHeader, AddHeader, DefaultHeader or SetContentType rather than assigning Headers directly. SetHeader always appends a new row; AddHeader and DefaultHeader add the header only if it isn't already present.
  • To read a response header, use req.(Response.Headers GetHeader 'content-type'). See GetHeader.
  • Jarvis adds the Server and Date headers when it sends the response, and Content-Encoding if it compresses the payload.

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
     ∇