Integration with Choice

Flow script

Consider the script of the flow below.
Also refer to the summary and the description of the elements for better understanding.

- from:
    uri: rest:post:/demo
    steps:
      - choice:
          otherwise:
            steps:
              - setBody:
                  expression:
                    constant:
                      expression: "Error: ID not found in the request body"
              - setHeader:
                  expression:
                    constant:
                      expression: "400"
                    name: CamelHttpResponseCode
          when:
            steps:
              - setBody:
                  expression:
                    constant:
                      expression: Success
              - setHeader:
                  expression:
                    constant:
                      expression: "200"
                    name: CamelHttpResponseCode
                expression:
                  jsonpath:
                    expression: $.model.Details[?(@['Random nonsense'] == 'New today')]

Summary

This flow sets up a route that responds to POST requests at the /demo endpoint. Depending on the content of the request, the flow performs a conditional check:

The condition checks if Random nonsense within $.model.Details is New today.

  • If the condition is true:

    • The response body is set to Success.

    • The CamelHttpResponseCode header is set to 200.

  • If the condition is false:

    • The response body is set to Error: ID not found in the request body.

    • The CamelHttpResponseCode header is set to 400.

The choice structure allows for flexible conditional routing, while setBody and setHeader allow dynamically setting the response body and headers based on the evaluated conditions.

Description of the elements

Below is the explanation of each element of the integration script:

  1. from

    Defines the entry point of the Camel route. Specifies that the route responds to HTTP POST requests at the /demo endpoint.

    - from:
        uri: rest:post:/demo
  2. steps

    Lists the actions to be executed when a request is received at the defined endpoint. In the example, it contains a conditional choice structure.

    steps:
      - choice:
  3. choice

    Defines a conditional structure that can have multiple paths, executing different sets of actions depending on the specified conditions.

    choice:
  4. otherwise

    Defines the set of actions to be executed if none of the when conditions are met. In this case, it sets the response body to an error message and the HTTP response code to 400 (Bad Request).

    otherwise:
      steps:
        - setBody:
            expression:
              constant:
                expression: "Error: ID not found in the request body"
        - setHeader:
            expression:
              constant:
                expression: "400"
              name: CamelHttpResponseCode
  5. setBody (otherwise)

    Defines the HTTP response body when the otherwise condition is met. Here, the body is set to the constant string Error: ID not found in the request body. [1]

    - setBody:
        expression:
          constant:
            expression: "Error: ID not found in the request body"
  6. setHeader (otherwise)

    Defines a specific HTTP header for the response when the otherwise condition is met. Here, the CamelHttpResponseCode header is set to 400.

    - setHeader:
        expression:
          constant:
            expression: "400"
          name: CamelHttpResponseCode
  7. when

    Defines a set of actions to be executed if a specific condition is met. In this case, the condition checks if the Random nonsense field within $.model.Details is equal to New today.

    when:
      steps:
        - setBody:
            expression:
              constant:
                expression: Success
        - setHeader:
            expression:
              constant:
                expression: "200"
              name: CamelHttpResponseCode
          expression:
            jsonpath:
              expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
  8. steps (when)

    Lists the actions to be executed if the when condition is met. In this case, it sets the response body to Success and the HTTP response code to 200 (OK).

    steps:
      - setBody:
          expression:
            constant:
              expression: Success
      - setHeader:
          expression:
            constant:
              expression: "200"
            name: CamelHttpResponseCode
        expression:
          jsonpath:
            expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
  9. setBody (when)

    Defines the HTTP response body if the when condition is met. Here, the body is set to the constant string Success.

    - setBody:
        expression:
          constant:
            expression: Success
  10. setHeader (when)

    Defines a specific HTTP header for the response if the when condition is met. Here, the CamelHttpResponseCode header is set to 200.

    - setHeader:
        expression:
          constant:
            expression: "200"
          name: CamelHttpResponseCode
  11. expression (when)

    Specifies the condition to be evaluated to determine if the when block should be executed. Uses a JSONpath expression to check if the Random nonsense field within $.model.Details is equal to New today.

    expression:
      jsonpath:
        expression: $.model.Details[?(@['Random nonsense'] == 'New today')]

1. A constant string is a sequence of characters whose value does not change. In the context of the integration, constant strings are used to set fixed values for the body and headers of HTTP responses, ensuring that these values remain consistent whenever the route is executed.
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]