Skip to content

Response Interceptor

thiagobustamante edited this page May 19, 2017 · 9 revisions

A Response Interceptor is a function that receives thr following parameters:

  • originalResponse: The response received from the destination API.
  • data: the body of the responsed received from the destination API.
  • request: the request received by gateway from the client.
  • response: the response to be sent by the gateway to the client.
  • callback: A callback function, following the convention: callback(error, value). Where value will be the new response body

Each interceptor must be defined on its own .js file.

Example:

/**
 * Where proxyReq and originalReq are request objects that follows the [express](http://expressjs.com) contract.
 * @param originalResponse The response received from the destination API.
 * @param data the body of the responsed received from the destination API.
 * @param request the request received by gateway from the client.
 * @param response the response to be sent by the gateway to the client.
 * @param callback A callback function, following the convention: callback(error, value). Where value will be the new response body
 */
module.exports = function(originalResponse, data, request, response, callback) {
   data = JSON.parse(data.toString('utf8'));
   callback(null, JSON.stringify(data));
};

You can configure a request interceptor middleware:

  • Admin Rest API: POST /midleware/interceptors/response
  • SDK: sdk.middleware.addResponseInterceptor(name, fileName);
  • CLI: treeGatewayConfig middleware responseInterceptor -a <name> ./filename.js
Clone this wiki locally