Content Based Routing

If you have a native API that is hosted at two or more endpoints, you can use the Content-based routing protocol to route specific types of messages to specific endpoints. You can route messages to different endpoints based on specific values that appear in the request message.

You might use this capability, for example, to determine which operation the consuming application has requested, and route requests for complex operations to an endpoint on a fast machine. For example, if your entry protocol is HTTP or HTTPS, you can select the Content-based routing. The requests are routed according to the content-based routing rules you create. You may specify how to authenticate requests.

In API Studio, the kind Route identifies this policy.

Example of a Content Based Routing policy:

kind: Route
apiVersion: api.webmethods.io/beta
metadata:
  name: contentBasedRouting
  version: 1.0
  namespace: sample
spec:
  default-endpoint:
    $ref: default_endpoint
  content-based-endpoints:
    - payloadIdentifier:
        - jPath: '$.customer.name'
        - xpath: '$.customer.id'
          namespaces:
          - prefix: ''
            URI: ''
        - regex: '$.customer.name'
      endpoint:
        $ref: content_based_endpoint:1.0
---
kind: HTTPEndpoint
apiVersion: api.webmethods.io/beta
metadata:
  name: default_endpoint
  version: 1.0
spec:
  url: http://localhost:3000/
  method: GET
  connectTimeout: 300
  readTimeout: 200
  sslConfig:
    keyStoreAlias: ""
    keyAlias: ""
    trustStoreAlias: ""
  serviceRegistryParameters:
    - name: ""
      value: ""
---
kind: HTTPEndpoint
apiVersion: api.webmethods.io/beta
metadata:
  name: content_based_endpoint
spec:
  url: http://localhost:4000/
  method: GET
  connectTimeout: 300
  readTimeout: 200
  sslConfig:
    keyStoreAlias: ""
    keyAlias: ""
    trustStoreAlias: ""
  serviceRegistryParameters:
    - name: ""
      value: ""

To invoke the Content Based Routing policy, you can specify:

Type Description
kind

Route. Defines the type of policy that is being configured.

apiVersion Denotes the version of the API in use.
metadata Denotes the metadata or details of the asset type.
It covers this metadata:
  • name. Name of the policy.
  • version. Version of the policy.
  • namespace. Namespace of the policy.
spec

You can configure the following policy specifications:

  • default-endpoint. Specifies the URI of the native API endpoint to route the request to. This endpoint is provided as $ref value in the format $ref: <endpoint name>: version
  • content-based-endpoints. Define a list of content-based routing rules that map specific payload values to target endpoints.

    Configure the following specs:

    • payloadIdentifier. Specify the payload identifier to locate the client, extract the custom authentication credentials from the request, and verify the client’s identity.
      You can use the following payload types:
      • jPath. Specify the JSONPath expression to extract values from a JSON payload.
      • XPath. Specify the XPath expression to extract values from an XML payload.

        Specify these details under namespaces:

        • prefix. Specify the namespace prefix of the payload expression to be validated.
        • URI. Specify the namespace URI of the payload expression to be validated.
    • regex. Specify a regular expression to match values in the payload for routing.
    • end-point. Specify the URI of the conditional endpoint to route the request to.

      This endpoint is provided as $ref value in the format $ref: <conditional endpoint name>: version.

To configure the default_endpoint, you can specify:

Type Description
kind

HTTPEndpoint. Defines the type of asset that is being configured.

apiVersion Denotes the version of the API in use.
metadata Denotes the metadata or details of the asset type.
It covers this metadata:
  • name. Name of the asset. Set it to default_endpoint
  • version. Version of the asset.
spec This denotes various asset properties that you can configure for the endpoint.

These are the endpoint specifications that can be configured:

  • URL. Specifies the URL for the default endpoint.
  • method. Specifies the available routing methods.
  • connectTimeout. Specifies the number of seconds after which connection timeout is initiated.
  • readTimeout. Specifies the number of seconds after which read timeout is initiated.
  • sslConfig. Specifies the SSL configurations.
    You can configure the following SSL details:
    • keyStoreAlias. Specify the keystore alias to identify the API call. This value (along with the value of Client Certificate Alias) is used for conducting an SSL client authentication.
    • keyAlias. Specify the alias of the private key, which should reside in the keystore specified by the keystore alias.
    • trueStoreAlias. Specify the alias for the truststore containing CA certificates used by API Studio to validate the native API’s SSL certificate.
  • serviceRegistryParameters. Specify values that are used for constructing the discovery service URI. Configure the name and value.

To configure the conditional_endpoints, you can specify:

Type Description
kind

HTTPEndpoint. Defines the type of asset that is being configured.

apiVersion Denotes the version of the API in use.
metadata Denotes the metadata or details of the asset type.
It covers this metadata:
  • name. Name of the asset. Set it to condition_endpoint
spec This denotes various asset properties that you can configure for the endpoint.

These are the endpoint specifications that can be configured:

  • URL. Specifies the URL for the default endpoint.
  • method. Specifies the available HTTP routing methods.

    Configure one of the following options:
    • GET
    • POST
    • PUT
    • DELETE
    • CUSTOM(default)
  • connectTimeout. Specifies the number of seconds after which connection timeout is initiated.
  • readTimeout. Specifies the number of seconds after which read timeout is initiated.
  • sslConfig. Specifies the SSL configurations.
    You can configure the following SSL details:
    • keyStoreAlias. Specify the keystore alias to identify the API call. This value (along with the value of Client Certificate Alias) is used for conducting an SSL client authentication.
    • keyAlias. Specify the alias of the private key, which should reside in the keystore specified by the keystore alias.
    • trueStoreAlias. Specify the alias for the truststore containing CA certificates used by API Studio to validate the native API’s SSL certificate.
  • serviceRegistryParameters. Specify values that are used for constructing the discovery service URI. Configure the name and value.