DataPower Gateway (Classic)
only

Writing switch condition scripts - DataPower Gateway (v5 compatible)

You write condition scripts for the switch policy in the DataPower® Gateway (v5 compatible) by using GatewayScript.

To reference variables in your switch conditions, use the form apim.getvariable('context.location.variable'), where context is the context that you want to reference, location is the location of the variable within that context, and variable is the name of the variable.

to define a switch condition based on the operation called, use one of the following forms:
- condition: "((request.verb==='GET')&&(api.operation.path==='/path-1'))"
- condition: "((api.operation.id=='Operation_ID'))"
where the context variables request.verb and api.operation.path retrieve the HTTP verb and the path segment of the API and operation that you want your case to apply to, while api.operation.id retrieves the operation ID of your operation, if one has been specified.

For information about the context variables you can use and how to reference them in your script, see API Connect context variables.

Example switch policy

- switch:
  version: 1.0.0
  title: switch
  case:
    - condition: message.status.code===200
      execute:
        - invoke:
          title: invoke
          timeout: 60
          verb: GET
          cache-response: protocol
          cache-ttl: 900
          target-url: 'https://example.com/1'
    - condition: ((request.verb==='GET')&&(api.operation.path==='/path-2'))||((request.verb==='GET')&&(api.operation.path==='/path-1'))
      execute:
        - invoke:
          title: invoke
          timeout: 60
          verb: GET
          cache-response: protocol
          cache-ttl: 900
          target-url: 'https://example.com/2'
    - otherwise:
      - set-variable:
        title: set-variable
        actions:
          - set: message.body
            value: Default result
            description: Set the default result for the otherwise case