Membrane API Gateway can be extended with small scripts and expressions. Scripts can read and change headers and bodies, build responses, transform JSON or steer the flow of a request.
All three languages share the same built-in variables and functions:
api:
port: 2000
flow:
- request:
- groovy:
src: |
header.put('X-Request-Method', method)
CONTINUE
target:
url: https://api.predic8.de
api:
port: 2000
flow:
- request:
- groovy:
src: |
header.put('X-Request-Method', method)
CONTINUE
target:
url: https://api.predic8.de
End a script with CONTINUE when it only changes values. Otherwise the value of the last expression may become the new body.
api:
port: 2000
flow:
- groovy:
src: |
Response.ok()
.contentType("application/json")
.header("X-Foo", "bar")
.body('{"success": true}')
.build()
api:
port: 2000
flow:
- groovy:
src: |
Response.ok()
.contentType("application/json")
.header("X-Foo", "bar")
.body('{"success": true}')
.build()
api:
port: 2000
flow:
- request:
- javascript:
src: |
// Returned object becomes the JSON body
({ id: params.get('id')?.[0], city: params.get('city')?.[0] })
- return:
contentType: application/json
api:
port: 2000
flow:
- request:
- javascript:
src: |
// Returned object becomes the JSON body
({ id: params.get('id')?.[0], city: params.get('city')?.[0] })
- return:
contentType: application/json
api:
port: 2000
flow:
- request:
- if:
test: isJSON() and jsonPath('$.total') > 100
flow:
- setHeader:
name: X-Priority
value: high
- response:
- setHeader:
name: X-Method
value: ${method}
target:
url: https://api.predic8.de
api:
port: 2000
flow:
- request:
- if:
test: isJSON() and jsonPath('$.total') > 100
flow:
- setHeader:
name: X-Priority
value: high
- response:
- setHeader:
name: X-Method
value: ${method}
target:
url: https://api.predic8.de
api:
port: 2000
flow:
- response:
- template:
contentType: text/plain
src: |
Method: ${method}
Path: ${path}
Date: ${new Date()}
- return:
status: 200
api:
port: 2000
flow:
- response:
- template:
contentType: text/plain
src: |
Method: ${method}
Path: ${path}
Date: ${new Date()}
- return:
status: 200
The Membrane distribution contains runnable tutorials. Start Membrane in the tutorial folder with the YAML file as argument and follow the instructions in the file.