Skip to content

REST Client with Basic Authentication

Use case

This documentation outlines the process of creating a REST client that interacts with a Customer service requiring Basic Authentication via a username and password.

Design

The solution consists of two main components:

  1. REST Client Route: This route contains the logic for calling the REST service. It handles the HTTP request, manages authentication headers, and processes the response from the Customer service, client.camel.yaml

  2. Example Usage: An additional route demonstrates how to invoke the REST client from another route, ensuring seamless integration and reuse.

YAML

client.camel.yaml
- route:
    id: route-rest-client
    description: Rest Client
    nodePrefixId: route-6cb
    from:
      id: from-4a52
      uri: direct
      parameters:
        name: rest-client
      steps:
        - setHeader:
            id: setHeader-20b4
            description: Set Authorization header
            name: Authorization
            expression:
              groovy:
                id: groovy-5740
                expression: >-
                  def username = "Aladdin"

                  def password = "OpenSesame"

                  return "Basic " +
                  Base64.getEncoder().encodeToString("$username:$password".bytes) 
        - setHeader:
            id: setHeader-b4dd
            description: Set Content-Type header
            name: Content-Type
            expression:
              constant:
                id: constant-ccbf
                expression: application/json
        - setHeader:
            id: setHeader-36d7
            description: Set Accept header
            name: Accept
            expression:
              constant:
                id: constant-5f78
                expression: application/json
        - log:
            id: log-b206
            message: ${headers}
        - to:
            id: to-d080
            disabled: false
            uri: vertx-http
            parameters:
              httpUri: http://localhost:8082/customer
              httpMethod: GET
        - log:
            id: log-fbd4
            message: "${body} "
example.camel.yaml
- route:
    id: route-demo
    nodePrefixId: route-242
    from:
      id: from-0956
      uri: timer
      parameters:
        repeatCount: "1"
        timerName: test
      steps:
        - to:
            id: to-7fd6
            uri: direct
            parameters:
              name: rest-client