HTTP Request-Response with Logger Example
This example application illustrates how to use Mule ESB to build a simple HTTP request-response application. After reading this document, and creating and running the example in Mule, you should be able to leverage what you have learned to create a very simple HTTP request-response application.
This example was designed to demonstrate interaction between an end user and Mule via an HTTP request, and Mule’s ability to log activity in the application.
This document describes the details of the example within the context of Mule Studio, Mule ESB’s graphical user interface (GUI). Where appropriate, the XML configuration accompanies the Studio interface screenshots. This document assumes that you are familiar with Mule ESB and the Mule Studio interface. To increase your familiarity with Mule Studio, consider completing one or more Mule Studio Tutorials.
In this example, a user calls the Mule application by submitting a request via her browser (i.e. entering a specific URL,
http://localhost:8084/echo). The application receives the request and returns the same payload, or "echoes", the response to the end user. In other words, when a user types
http://localhost:8084/echo into the address bar of her browser, Mule returns a message in the browser that reads,
/echo (see image below, left); if she enters
http://localhost:8084/moon, Mule responds with
/moon (below, right).
There are two functions the Echo example application illustrates:
As with this HTTP Request-Response example, you can create template applications straight out of the box in Mule Studio or Mule Standalone (Mule ESB without Studio). You can tweak the configurations of these use case-based templates to create your own customized applications in Mule.
Follow the procedure below to create, then run the HTTP Request-Response with Logger application in Mule ESB.
Create, then run the example application in Mule Studio.
Open your Web browser, type
http://localhost:8084/echoin the address bar, then press enter.
Your browser presents a message that reads,
In your browser’s address bar, replace the word
echowith the word
moon, then press enter.
Your browser presents a message that reads,
The HTTP Request-Response with Logger example application consists of one, simple flow which receives end user HTTP requests, logs messages’ payloads, and returns the payloads to end users as HTTP responses.
The sections below elaborate further on the configurations of the application and how it works to respond to end user requests.
This flow makes use of three building blocks to receive, process and respond to an end user requests. When an end user request encounters the application, the first building block it meets is the request-response HTTP Inbound Endpoint. Because it has a two-way message exchange pattern, this HTTP endpoint is responsible for both receiving requests from, and send sending responses to, the end user.
Next, the flow uses a Logger Component to log the message payload in the application’s log files. The logger component uses a MEL Expression to determine what information in, or about, the message it should log. In this case, because it needs to log the message payload, the instructions to log read
About to echo #[message:payload].
1 <logger message="About to echo #[message:payload]" level="INFO" doc:name="Logger"/>
Finally, Mule moves the message to an Echo Component which simply returns the message payload as the response to an end user. In other words, the response echoes the request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd "> <flow name="HTTPRequest-Reponse_with_Logger" doc:name="HTTPRequest-Reponse_with_Logger" doc:description="Accepts HTTP requests, logs the message payload, then returns a response with the same payload as the request (i.e. echoes)."> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" doc:name="HTTP" doc:description="Process HTTP requests or responses."/> <logger message="About to echo #[message:payload]" level="INFO" doc:name="Logger"/> <echo-component doc:name="Echo"/> </flow> </mule>