Skip to main content

Request Transformation

Request Transformation gives you the ability to rewrite your requests before they are forwarded to your service instances. You can provide custom logic written in Javascript to transform headers, method, uri, and even the body of your request before it is forwarded by Diffy to your service instances.

Injection Points

There are 4 places - all, primary, secondary, candidate - in the Diffy topology where request transformation can happen. We refer to these as Injection Points. We can access these injection points by clicking on the JS button in the Diffy UI as highlighted button in the image below. Request Transformation We can now select an injection point by clicking on the corresponding node in the transformation dialog and write the corresponding transformation in the text area.

All

The tranformation logic written for all is applied to all requests before being multicast to primary, secondary, and candidate.

Primary, Secondary, Candidate

The transformation written for primary, secondary, or candidate will only be applied to the copy of the request being sent to that specific service instance. i.e. The transformation written for candidate will only tranform the traffic sent to the candidate instance and will not affect the traffic sent to primary and secondary in any way.

Grammar

The transformation is written as a self-contained Javascript function that takes a request object as its input and returns a request object as its output. The Javascript environment for request transformations does not allow access to external libraries to imported via require or import. The same rule applies to all external dependencies including network and filesystem. The sturcture of a request object is demonstrated in the following example transformation:

(request) => {
const {
uri, // String like - '/users/321/name?includeLastName=true'
method, // String like - 'GET', 'POST'
headers, // Json object with string values like - { "auth_token" : "SOME***SECRECT***TOKEN" }
body // String like - 'hello world', '{ "hello" : "world"}', '<html>hello world</html>'
} = request;

return {
uri,
method,
headers,
body
}
}

The above example is a noop transformation that returns an unmodified request.

For a detailed discussion on the motivation for Request Transformation, please refere to this blog post.