-
Notifications
You must be signed in to change notification settings - Fork 43
Request Interceptor
Thiago Bustamante edited this page May 29, 2017
·
9 revisions
A Request Interceptor is a function that receives two request objects as parameters.
- 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 userReq the request received by the gateway from the client.
*/
module.exports = function(proxyReq, userReq) {
// 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!');
};
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