Skip to content

responseBodyTransformer

Thiago da Rosa de Bustamante edited this page Mar 27, 2018 · 2 revisions

This interceptor needs to receive an options object containing the jsonata expression to be applied for transformation.

The following configuration maps all endpoints from http://httpbin.org and applies a transformation on the response for the operation GET on path /get (GET http://httpbin.org/get).

---
name: TestInterceptedAPI
version: 1.0.0
path: "/responsetransform"
group:
- id: Group1
  description: Endpoints Group One
  member:
  - path:
    - get/
    method:
    - GET
proxy:
  target:
    host: http://httpbin.org
  timeout: five seconds
interceptor:
  response:
    - middleware:
        name: responseBodyTransformer
        options:
          expression: >
            {
              'argumentNames': $sort($keys(args), function($l, $r) {
                  $l < $r
                })
            }
      group:
        - Group1

It will transform the original response returned by the API. For the given URL:

http://httpbin.org/get?arg1=1&param2=2

Httpbin returns:

{
  "args": {
    "arg1": "1", 
    "param2": "2"
  }, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, sdch", 
    "Accept-Language": "en-US,en;q=0.8,pt;q=0.6", 
    "Connection": "close", 
    "Cookie": "_ga=GA1.2.1324135042.1476879149; _gauges_unique_year=1; _gauges_unique=1", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
  }, 
  "origin": "189.13.176.105", 
  "url": "http://httpbin.org/get?arg1=1&param2=2"
}

And our gateway configuration transforms it to:

{
    "argumentNames": [
        "param2", "arg1"
    ]
}

when accessing http://<gatewayaddress>/responsetransform?get?arg1=1&param2=2

Clone this wiki locally