Contact Us 1-800-596-4880

Using Anypoint Code Builder to Configure HTTP Connector - Mule 4

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

To use Anypoint Code Builder to configure a connector or module, create a basic integration project in Anypoint Code Builder, add connectors and other components to your Mule application to process your business logic, and configure the attributes.

Before You Begin

Before creating an integration project, you must:

To use this connector, you must be familiar with:

  • The connector’s API

  • Anypoint Connectors

  • Mule runtime engine (Mule)

Creating a New Integration Project

To create a new integration project:

  1. In the activity bar of the IDE, click the Anypoint Code Builder icon (Anypoint Code Builder) icon.

  2. From Quick Actions, click Develop an Integration:

    Develop an Integration link highlighted in the Getting started section

  3. Complete the following in the Develop an Integration form:

    Attribute Value

    Project Name

    Unique name for your project.

    This name is used as the title and name of the integration project file. For example, if the project name is "Salesforce Integration," the project file name is salesforce-integration.

    Project Location

    Your home directory or another directory you create. Click Browse to select a different directory for the integration project.

    Don’t create the project within another project directory.

  4. Select the type of project to create by selecting either the Empty Project or Template or Example card.

  5. Select the Mule runtime and Java Version for your app.

  6. Click Create Project.

    Develop an Integration Flow window

Adding the Connector to Your Integration Project

Anypoint Connectors provide operations for retrieving, modifying, and sending data to and from systems.

In addition to the built-in connectors that Anypoint Code Builder provides, you can download many other connectors from Anypoint Exchange.

To import a connector from Exchange and add it to your configuration:

  1. In the Explorer view, open the configuration XML file for your project, such as my-project-name.xml.

  2. Click the Show Mule graphical mode icon (Show Mule graphical mode) icon in the activity bar to open the canvas UI if it doesn’t open automatically.

  3. Add the connector the same way you added other components from the canvas UI:

    1. In the canvas UI, click the Add component icon (Add component) icon.

    2. In the Add Component panel, click Connectors.

    3. Click the connector name and then click the operation to add, such as Publish:

      Add Publish operation from Anypoint MQ Connector

      If the connector is not available locally, click the Search in Exchange icon (Search in Exchange) toggle:

      Search in Exchange toggle

      1 Search locally
      2 Search in Exchange
    4. Select the connector to add to your project.

    5. Select the operation from the Add Component panel.

Adding a connector to an integration project does not make that connector available to other projects in your Anypoint Code Builder workspace.

Configuring the Trigger For the Flow

A trigger (source) starts the flow when specified conditions are met. If a connector has its own associated sources, you can use a connector-specific trigger to initiate the flow. Each connector trigger has a unique configuration.

You can configure the following sources for this connector:

  • HTTP > Listener

    Initiates a flow each time it receives a request on the configured host and port

  • Scheduler

    Initiates a flow when a time-based condition is met

For example, to configure an HTTP Listener trigger, follow these steps:

  1. Click the Listener component on the canvas UI.

  2. (Optional) Click the edit icon to change the name of the Listener component.

  3. (Optional) If you created a configuration file for the connector, select it.

  4. In the General tab, in the Path field, enter the relative path for the path set in the HTTP configuration element. See Configure Paths.

  5. Select the Advanced tab to configure the following attributes:

    Attribute Description Required

    Allowed methods

    To allow all HTTP methods (default), do not define this attribute. To restrict certain methods, specify all allowed HTTP methods, for example, GET, POST, as a comma-separated list.

    No

    Response streaming mode

    Whether to use streaming when the response is sent.

    No

    Output MIME type

    MIME type of the payload that this component outputs.

    No

    Parameters

    Appends a key-value pair for a reader property to the value of the Output MIME attribute. Multiple key-value pairs are allowed.

    No

    Output encoding

    Encoding of the payload that this component outputs.

    No

    Streaming Strategy

    Configures the way data streams are consumed in Mule apps. This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Configuring the Streaming Strategy in XML.

    No

    Primary node only

    Select this option to execute the Listener trigger on the primary node only when running in a cluster.

    No

    Redelivery policy

    Configures the redelivery policy for executing requests that generate errors. You can add a redelivery policy to any source in a flow. This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Redelivery Policy.

    No

    Reconnection strategy

    When an operation in a Mule application fails to connect to an external server, the default behavior is for the operation to fail immediately and return a connectivity error. You can modify the default behavior by configuring the reconnection strategy for the operation. This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Reconnect, Reconnect Forever, and Configuring the Reconnection Strategy in XML.

    No

    Response

    See HTTP Response Attributes.

    No

    Error Response

    HTTP error response returns a status code for the error.

    No

    Body

    Body of the error message.

    No

    Headers

    Headers to include in the error message.

    No

    Status code

    Status code of the error response, for example, 500.

    No

    Reason phrase

    Reason for the error, for example, Gateway Timeout or Internal Server Error.

    No

For details about the Listener configuration attributes, see Listener Configuration.

Configuring a Connector in XML

Although you can manually code a Mule app in XML, it is more efficient to use Anypoint Code Builder:

  • If you manually code a Mule runtime engine (Mule) app in XML from the Anypoint Code Builder XML editor, you can access the connector from your app by adding reference statements to both your XML Mule flow and the Apache Maven pom.xml file.

  • If you add the connector in Anypoint Code Builder, Studio automatically populates the XML code with the connector’s namespace and schema location and it also adds a dependency to the pom.xml file.

This example shows how the namespace statements are placed in the <mule> XML block:

<mule xmlns:{lc-connector-name}="http://www.mulesoft.org/schema/mule/{lc-connector-name}"
	xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:file="http://www.mulesoft.org/schema/mule/file"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
	http://www.mulesoft.org/schema/mule/core/current/mule.xsd
	http://www.mulesoft.org/schema/mule/file
	http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
	http://www.mulesoft.org/schema/mule/http
	http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
	http://www.mulesoft.org/schema/mule/ee/core
	http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
	http://www.mulesoft.org/schema/mule/{lc-connector-name}
	http://www.mulesoft.org/schema/mule/{lc-connector-name}/current/mule-{lc-connector-name}.xsd">

The Apache Maven pom.xml file generated by Anypoint Code Builder contains dependencies for a Mule app. If you code a Mule app manually, include this XML snippet in your pom.xml file to enable access to this connector:

<dependency>
  <groupId>{group-id-exchange}</groupId>
  <artifactId>{artifact-id-exchange}</artifactId>
  <version>x.x.x</version>
  <classifier>mule-plugin</classifier>
</dependency>

Replace x.x.x with the version that corresponds to the connector you are using.

To obtain the most up-to-date pom.xml file information:

  1. Go to Anypoint Exchange.

  2. In Exchange, click Login and supply your Anypoint Platform username and password.

  3. In Exchange, search for the connector.

  4. Select the connector.

  5. Click Dependency Snippets.

Adding Components to Your Project

Add components to your integration project to build a flow:

  1. In the Explorer view, open the XML file for your project, such as my-project-name.xml.

  2. Select Build a Flow from the start card to create an empty flow:

    Canvas UI with starting card with the option to start from scratch, build a flow, or build a subflow

  3. Change the name of the flow:

    1. Click Flow.

    2. Click the edit icon.

      Arrow pointing to the flow name edit pencil icon

    3. Enter the flow name, and click the checkmark.

    4. In the canvas UI, click the Add component icon (Add component) icon.

    5. In the Add Component panel, search for and select your component from the results. The following example shows the Listener component from the HTTP search results:

      Listener component highlighted in the Add Component section

      The configuration XML file now includes the XML for the HTTP Listener into the <flow/> element, for example:

      <flow name="getFlights" >
        <http:listener path="" config-ref="" doc:name="Listener" doc:id="ojzfry" />
      </flow>

Adding a Global Configuration

Add another component, this time using the XML configuration menu. For example, add the <http:listener-config/> component from a snippet.

For more information about snippets, see Working with Code Snippets.

  1. In the configuration XML, place your cursor before the <flow/> element and type http.

    Ensure that the cursor is not inside the <flow/> element.

  2. Type http, and select the http:listener-config snippet:

    http:listener-config snippet highlighted in the configuration XML menu

    The snippet adds the following code:

<http:listener-config name="HTTP_Listener_config" >
  <http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>

Configuring Authentication for the Connector

The Anypoint Connector for HTTP (HTTP Connector) Request operation supports connecting a Mule client app to a service that requires any of the following types of authentication:

  • Basic Authentication

  • Digest Authentication

  • NTLM Authentication

  • OAuth2 Authorization Code Grant Type Authentication

HTTP XML Authentication Setup

Configure HTTP Connector authentication in the XML editor. These are the XML values for HTTP Connector authentication:

Authentication Type XML Value

Basic Authentication

<http:authentication >
  <http:basic-authentication
  username="UN" password="PW"/>
</http:authentication>

NTLM Authentication

<http:authentication >
  <http:ntlm-authentication
  domain="DM" workstation="WS"
  username="UN" password="PW" />
</http:authentication>

Digest Authentication

<http:authentication >
  <http:digest-authentication
  username="UN" password="PW"/>
</http:authentication>

OAuth 2.0

<http:listener-config name="HTTP_Listener_Configuration"
                      host="0.0.0.0" port="8081" basePath="/github"/>
<http:request-config name="HTTP_Request_Configuration"
                     protocol="HTTPS" host="api.github.com" port="443">
    <http:authentication>
        <oauth:authorization-code-grant-type
        externalCallbackUrl="http://myapp.mycompany.com:8082/callback"
        localAuthorizationUrl="http://localhost:8082/login"
        authorizationUrl="https://github.com/login/oauth/authorize"
        clientId="CLIENT_ID"
        clientSecret="CLIENT_SECRET"
        tokenUrl="https://github.com/login/oauth/access_token" />
    </http:authentication>
</http:request-config>

Configuring the Component Attributes

After you add components to your integration project, configure the attributes in the component UI, or configure the attributes directly in the XML code editor.

Connector XML tags start with a namespace followed by an operation, following the syntax namespace:operation, such as <db:select/> for a Select operation in Anypoint Connector for Database (Database Connector).

You can use autocomplete from an expression (fx) field for a component to select DataWeave functions from the Core module and other values accepted in this field.

Configuring the HTTP Listener

The Anypoint Connector for HTTP (HTTP Connector) Listener component enables you to set up an HTTP server and trigger flows when receiving HTTP requests.

To configure the HTTP Listener attributes in the UI, see Configuring the Trigger For the Flow.

Configuring the Basic Security Filter

The Basic security filter component authenticates received HTTP requests. Use this component after a Listener component in your integration flow.

  1. Click the Basic security filter component on the canvas.

  2. In the General tab, configure the following attributes:

    UI Field Description Required

    Realm

    Authentication realm.

    Yes

    Security Provider

    This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Basic Security Filter.

    No

    Attributes

    HTTP Request attributes defined in the HTTP Listener source to check the Authorization header.

    No

Configuring the HTTP Request

The Anypoint Connector for HTTP (HTTP Connector) Request operation enables you to consume an external HTTP service using the default GET method. Otherwise, you can choose which methods the operation accepts.

To configure the attributes for HTTP Request in the Anypoint Code Builder UI:

  1. Click the Request component on the canvas.

  2. In the General tab, configure the following attributes:

    UI Field Description Required

    Method

    To allow all HTTP methods (default), do not define this attribute. To restrict certain methods, specify all allowed HTTP methods, for example, GET, POST, as a comma-separated list.

    No

    URI Settings URL

    URL to send the request to.

    No

    URI Settings Path

    Path for where to send the request.

    No

    Request Body

    Body of the request response message.

    No

    Request Headers

    Headers to include in the response message.

    No

    URI Parameters

    URI parameters used to create the request.

    No

    Query Parameters

    Query parameters to include in the request.

    No

    Correlations ID

    Defines whether to include an outbound correlation ID in the request.

    No

  3. Select the Advanced tab to configure the following attributes:

    UI Field Description Required

    Output MIME type

    MIME type of the payload this component outputs.

    No

    Parameters

    Appends a key-value pair for a reader property to the value of the Output MIME attribute. Multiple key-value pairs are allowed.

    No

    Output encoding

    Encoding of the payload this component outputs.

    No

    Streaming Strategy

    This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Configuring the Streaming Strategy in XML.

    No

    Follow redirects

    Specifies whether or not to follow redirects.

    No

    Send body mode

    Specifies whether the request contains a body. Select Auto to specify that the behavior is determined by the defined allowed HTTP methods (GET, HEAD, and OPTIONS do not send a body).

    No

    Request streaming mode

    Defines whether the request is sent using streaming.

    No

    Response timeout

    Specifies the maximum time in milliseconds that the request element blocks the execution of the flow waiting for the HTTP response.

    No

    Response validator

    Configures default error handling for the request. This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Success Status Code Validator and Failure Status Code Validator.

    No

    Target Variable

    Name of the variable on which the operation’s output is placed.

    No

    Target Value

    An expression that’s evaluated against the operation’s output. The outcome of that expression is stored in the target variable.

    No

    Error Mappings

    This attribute is not currently enabled.

    No

    Reconnection Strategy

    This attribute is not currently enabled in the configuration UI. Configure it in the XML editor. See Reconnect, Reconnect Forever, and Configuring the Reconnection Strategy in XML.

    No

Configuring the Load Static Resource

The static resource serves static content to use with HTTP. The request path is used to look up the resource.

To configure the attributes for HTTP Load static resource in the Anypoint Code Builder UI:

  1. Click the Load static resource component on the canvas.

  2. In the General tab, configure the following attributes:

    UI Field Description Required

    Resource base path

    The base path of the resource that serves up the content, for example, /Users/maxthemule/ resources.

    Yes

    Default file

    The default file to serve when a directory is specified. The default is index.html.

    No

    Attributes

    The HTTP request attributes defined in the HTTP Listener source to check the required resources.

    No

  3. Select the Advanced tab to configure the following attributes:

    UI Field Description Required

    Output MIME type

    MIME type of the payload this component outputs.

    No

    Parameters

    Appends a key-value pair for a reader property to the value of the Output MIME attribute. Multiple key-value pairs are allowed.

    No

    Output encoding

    Encoding of the payload this component outputs.

    No

    Target Variable

    Name of the variable that stores the operation’s output.

    No

    Target Value

    Expression that evaluates the operation’s output. The expression outcome is stored in the target variable.

    No

    Error Mappings

    This attribute is not currently enabled.

    No

XML Examples

Configuring the Reconnection Strategy in XML

The following is an XML example of a standard reconnection strategy setting the count of reconnection attempts before failing to 5 and setting failsDeployment to true, which causes the application deployment to fail if the connection test fails. Because a frequency is not specified, the retry interval is the default of every 2000 ms:

<db:config name="Database_Config" doc:name="Database Config" doc:id="79ae8d0f-e0af-4901-961d-b80befbf3f86" >
	<db:data-source-connection >
		<reconnection failsDeployment="true" >
           <reconnect count="5"/>
        </reconnection>
	</db:data-source-connection>
</db:config>

The following is an XML example of a reconnect forever strategy where the connector attempts to reconnect every 4000 ms for as long as the application runs:

<db:config name="Database_Config1" doc:name="Database Config" doc:id="c0aaf1dd-ab6e-49ee-9e4d-8b5799945cc9">
    <db:data-source-connection>
        <reconnection failsDeployment="true">
            <reconnection>
                <reconnect-forever frequency="4000"/>
            </reconnection>
        </reconnection>
    </db:data-source-connection>
</db:config>

Configuring the Streaming Strategy in XML

You can configure how Mule handles streams with streaming strategies.

File-Stored Repeatable Stream

The following is an XML example of reading a file that is configured with a 1 MB buffer:

<file:read path="bigFile.json">
  <repeatable-file-store-stream
    inMemorySize="1"
    bufferUnit="MB"/>
</file:read>

If you consistently process a file that’s no larger than 10 KB, you can save memory by reducing the inMemorySize attribute:

<file:read path="smallFile.json">
  <repeatable-file-store-stream
    inMemorySize="10"
    bufferUnit="KB"/>
 </file:read>

In-Memory Repeatable Stream

The following XML example shows an in-memory repeatable stream with a 512 KB initial size, which grows at a rate of 256 KB and allows up to 2000 KB (2 MB) of content in memory:

<file:read path="exampleFile.json">
  <repeatable-in-memory-stream
    initialBufferSize="512"
    bufferSizeIncrement="256"
    maxInMemorySize="2000"
    bufferUnit="KB"/>
</file:read>

Testing an HTTP Listener Connection

To test an HTTP Listener connection, click Test Connection in the configuration XML:

HTTP Listener Test Connection link in the configuration XML

The status bar shows the progress:

  • Verifying connection inbound-request indicates that the test is in progress.

  • Connection is valid indicates a successful connection.

  • Invalid Connection Got status code: 500 when trying to resolve a Mule Runtime operation indicates a connection error.

    A common code 500 error is port 8081: Address already in use. For port conflicts, configure a different port, such as 8082, and retest the connection.

Before publishing, run your application in debug mode.

Creating a New Configuration XML File

Anypoint Code Builder creates the initial Mule configuration XML file in src/main/mule within the Mule project. This file contains global configurations and flow structures.

You can create additional configuration files, for example, to store global configurations.

To create a new XML configuration file:

  1. In the Explorer view, right-click the mule folder (src/main/mule) and select New File.

    Alternatively, use the menu.

    Show me how

    Select the mule folder, and then:

    • In the desktop IDE, select File > New Mule Configuration File.

    • In the cloud IDE, click the (menu) icon, and select File > New File.

  2. Enter a name for the new file with the .xml extension.

  3. In the Editor view for the new file, press Ctrl+Space to display available options.

  4. Select Empty mule configuration:

    New Mule configuration option in auto-correct

    This command adds the following code to the file:

    <?xml version="1.0" encoding="UTF-8"?>
    <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    
    </mule>
View on GitHub