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 Anypoint 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 Anypoint Studio interface. To increase your familiarity with Anypoint Studio, consider completing one or more Anypoint 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 other example templates, you can create template applications straight out of the box in Anypoint Studio or, in this case, also in Mule Standalone where this example is called echo. You can tweak the configurations of these use case-based examples to create your own customized applications in Mule.
Follow the procedure below to create, then run the HTTP Request-Response with Logger application.
Create, then run the example application in Anypoint Studio or Standalone.
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 <?xml version="1.0" encoding="UTF-8"?> <mule version="EE-3.5.0" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" 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.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 doc:name="EchoFlow" name="EchoFlow"> <http:inbound-endpoint doc:description="Process HTTP requests or responses." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8084"/> <logger doc:name="Logger" level="INFO" message="About to echo #[message.payload]"/> <echo-component doc:name="Echo"/> </flow> </mule>