-
Notifications
You must be signed in to change notification settings - Fork 44
Response Interceptor
thiagobustamante edited this page May 29, 2017
·
9 revisions
A Response Interceptor is a function that receives the following parameters:
- body: the body of the response received from the destination API.
- headers the response headers to be sent by the gateway to the client.
- request: the request received by gateway from the client.
- callback: A callback function, like: callback(error, body, newHeaders). Where body will be the new response body and newHeaders inform a list of headers to be included into the response.
Each interceptor must be defined on its own .js file.
Example:
/**
* Where request is the object created by [http](https://nodejs.org/api/http.html) module.
* @param body the body of the response received from the destination API.
* @param headers the response headers to be sent by the gateway to the client.
* @param request the request received by gateway from the client.
* @param callback A callback function, like: callback(error, body, newHeaders, removeHeaders). Where body will be the new response body,
* newHeaders inform a list of headers to be included into the response and removeHeaders a list of headers to be suppressed.
*/
module.exports = function(body, headers, request, callback) {
var data = JSON.parse(body.toString('utf8'));
var newHeaders = {
mySpecialHeader: 'header value',
myOtherSpecialHeader: 'header value 2'
};
callback(null, data, newHeaders, ['excludedHeader']);
};If the response already includes one of those headers, they will be overwritten.
Note that to be able to override a response body, you must set the parseResBody to true on the proxy config.
You can configure a request interceptor middleware through:
- Admin Rest API:
POST /midleware/interceptors/response - SDK:
sdk.middleware.addResponseInterceptor(name, fileName); - CLI:
treeGatewayConfig middleware responseInterceptor -a <name> ./filename.js