Skip to content

requestBodyTransformer

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.

This interceptor requires that you set parseReqBody: true in your proxy configuration.

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

---
name: TestInterceptedAPI
version: 1.0.0
path: "/requesttransform"
group:
- id: Group1
  description: Endpoints Group One
  member:
  - path:
    - post/
    method:
    - POST
proxy:
  target:
    host: http://httpbin.org
  timeout: five seconds
parseReqBody: true
interceptor:
  request:
    - middleware:
        name: requestBodyTransformer
        options:
          expression: >
            {
              'name': Account.'Account Name',
              'myPurpleProducts':Account.Order[*].Product[Description.Colour='Purple']
            }
      group:
        - Group1

It will transform the original request received by the Gateway. For Example:

{
  "Account": {
    "Account Name": "Firefly",
    "Order": [
      {
        "OrderID": "order103",
        "Product": [
          {
            "Product Name": "Bowler Hat",
            "ProductID": 858383,
            "SKU": "0406654608",
            "Description": {
              "Colour": "Purple",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.75
            },
            "Price": 34.45,
            "Quantity": 2
          },
          {
            "Product Name": "Trilby hat",
            "ProductID": 858236,
            "SKU": "0406634348",
            "Description": {
              "Colour": "Orange",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.6
            },
            "Price": 21.67,
            "Quantity": 1
          }
        ]
      }
    ]
  }
}

And our gateway configuration transforms it to:

{
  "name": "Firefly",
  "myPurpleProducts": [
    {
      "Product Name": "Bowler Hat",
      "ProductID": 858383,
      "SKU": "0406654608",
      "Description": {
        "Colour": "Purple",
        "Width": 300,
        "Height": 200,
        "Depth": 210,
        "Weight": 0.75
      },
      "Price": 34.45,
      "Quantity": 2
    }
  ]
}
Clone this wiki locally