Contact Us 1-800-596-4880

Consume a REST Service Example - Mule 4

In the following example, you configure Anypoint Connector for HTTP (HTTP Connector) to consume a REST service by setting up the HTTP*Listener source and Request operation.

To accomplish this example, you must:

  • Consume a REST API service using default HTTP configurations.

  • Test the HTTP request.

  • Edit the configuration to make secure HTTPS requests.

  • Test the HTTPS request.

The following screenshot shows the Anypoint Studio app flow for this example:

Consume a REST API Service flow
Figure 1. Consume a REST API Service flow

Create the Mule Application

To create the Mule flow:

  1. In the Mule Palette view, select the HTTP Listener source and drag it on to the canvas.
    The source initiates the flow by listening for incoming HTTP message.

  2. On the Listener configuration screen, optionally change the value of the Display Name field.

  3. Set the Path field to /trigger.

  4. Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of the HTTP Listener in the app.

  5. On the General tab, configure the following fields:

    • Host
      All Interfaces [0.0.0.0] (default)

    • Port
      8081

  6. Click OK.

    HTTP Listener global configuration
    Figure 2. HTTP Listener global configuration
    HTTP Listener configuration
    Figure 3. HTTP Listener configuration
  7. Drag an HTTP Request operation to the right of HTTP Listener.

  8. Set the URL field to http://jsonplaceholder.typicode.com/users.

    HTTP Request configuration
    Figure 4. HTTP Request configuration
  9. Save your Mule app.

  10. Click the project name in Package Explorer and then click Run > Run As > Mule Application.

  11. Open a web browser, and go to localhost:8081/trigger.
    The Listener hears the request on port 8081, and starts the app. The list of users appears in the browser.

  12. Return to the Request configuration screen and set the URL field to https://jsonplaceholder.typicode.com/users to use the HTTPS protocol.

  13. Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of the HTTP Request in the app.

  14. Set the Port field to 443.

  15. Click OK.

    HTTP Request global configuration
    Figure 5. HTTP Request global configuration
  16. Save and run your Mule app.

  17. Open a web browser, and go to localhost:8081/trigger.
    The Listener hears the request on port 8081, and starts the app. The list of users appears in the browser.

XML for Consuming a REST API Service

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" >
		<http:request-connection port="443" />
	</http:request-config>
	<flow name="Flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/trigger"/>
		<http:request method="GET" doc:name="Request"  config-ref="HTTP_Request_configuration" url="https://jsonplaceholder.typicode.com/users"/>
	</flow>
</mule>
View on GitHub