Core
Write Plugins v1.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

Request #

Object representation of a Kuzzle API call.

That object is continuously updated to reflect the current state of the request, during its entire lifecycle.

For more information about this object, refer to its technical documentation.


Response headers #

Network protocol specific headers can be added to the response. If the protocol supports it, these headers are forwarded in the response sent to the client.

As Kuzzle supports the HTTP protocol natively, the Request object handles HTTP headers special cases. Other network protocols headers are stored in raw format, and protocols have to handle their own specific headers manually.

To customize the response content, read the RequestResponse documentation.


Constructor #

/

Deprecated since 1.2.0

new Request(request, data, [options]);
Available since 1.2.0
new Request(data, [options]);

ArgumentsTypeDescription
requestRequestA source request to inherit from
dataobjectAPI call, following the same format than non-HTTP API calls
optionsobjectAdditional request context

options #

The options argument accepts the following parameters:

OptionsTypeDescription
connectionobject
Available since 1.4.1
Connection information (see the connection object documentation)
connectionIdstring
Deprecated since 1.4.1
Connection unique identifier
errorKuzzleError, ErrorSets the request response with the provided error
requestIdstringUser-defined request identifier
result\*Sets the request response with the provided result, and the request status is set to 200
statusintegerRequest status, following the HTTP error code standard

Properties #

Read-only:

PropertyTypeDescription
contextRequestContextGeneral request information (logged user, network information, ...)
errorKuzzleErrorRequest error
inputRequestInputInput request representation
responseRequestResponseSerialized request response
result\*Request result
timestampintegerRequest creation timestamp, in Epoch-millis format

Writable:

PropertyTypeDescription
idstringUser-defined request unique identifier
statusintegerRequest status code

clearError #

Clears the error: sets the error property to null, and the request status to 200.


serialize #

Serializes the request into into a pair of objects that can be sent across the network, and then used to rebuild another equivalent Request object.

Example #

const foo = request.serialize();
const bar = new context.constructors.Request(foo.data, foo.options);

setError #

Adds an error to the request.

The request status is also updated to the error status.

Argument #

setError(error);

ArgumentsTypeDescription
errorKuzzleErrorRequest error

If a KuzzleError object is provided, the request's status attribute is set to the error one.

Otherwise, the provided error is embedded into a InternalError object, and the request status is set to 500.


setResult #

Sets the request result.

Arguments #

setResult(result, [options]);

ArgumentsTypeDescription
result\*Request result
optionsobjectOptional result configuration

options #

The options argument accepts the following parameters:

OptionsType (default)Description
headersobject (null)Network specific headers. Shortcut to the response header functions
rawboolean (false)If true, instead of a standard kuzzle response, the result is sent as is to the client, without being interpreted
statusinteger (200)Request status

Example #

Send a PDF

async sendPdf (request) {
  const file = fs.readFileSync('./file.pdf');

  request.setResult(null, {
    raw: true,
    headers: {
      'Content-Length': file.length.toString(),
      'Content-Type': 'application/pdf',
      'Content-Disposition': `attachment; filename="file.pdf"`,
      'Cache-Control': 'no-cache'
    }
  });

  return file;
}