Skip to content

Request Interceptor

thiagobustamante edited this page May 27, 2017 · 9 revisions

A Request Interceptor is a function that receives two request objects as parameters. Both of them follow the express contract for the request object.

  • The first parameter represents the request that is being created by the proxy engine targeting the destination API.
  • The second parameter represents the request received by the gateway from the client.

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

Example:

/**
 * Where proxyReq and userlReq are request objects created by [http](https://nodejs.org/api/http.html) module.
 * @param proxyReq the request that is being created by the proxy engine targeting the destination API.
 * @param userlReq the request received by the gateway from the client.
 */
module.exports = function(proxyReq, userlReq) {
   // you can update headers
   proxyReq.headers['Content-Type'] = 'text/html';
   // you can change the method
   proxyReq.method = 'GET';
   // you can munge the bodyContent.
   proxyReq.body = proxyReq.body.toString().replace(/losing/, 'winning!');
   return proxyReq;
};

Note that to be able to access the request body, you must set the parseReqBody to true on the proxy config.

You can configure a request interceptor middleware through:

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