Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mule-HTTP Request and Response Example

DZone's Guide to

Mule-HTTP Request and Response Example

Here, I'm going to explain handling HTTP requests and responses in Mule, using AnypointStudio version 6.0 and Mule version 3.8.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Here, I'm going to explain handling HTTP requests and responses in Mule, using AnypointStudio version 6.0 and Mule version 3.8.

(Used: HTTP Endpoint and Logger in the flow.)

1. Drag and drop the HTTP endpoint in the Mule flow and configure the HTTP listener configuration.

<http:listener-config doc:name="HTTP Listener Configuration" host="0.0.0.0" name="HTTP_Listener_Configuration" port="7000"/>


We are using port number 7000.

Map the listener configuration to the HTTP endpoint which we have created.

<http:listener config-ref="HTTP_Listener_Configuration" doc:name="Receive HTTP request" path="/*"/>


2. Read the request from the URL, will be using set payload to do that.

<set-payload doc:name="Set Payload" value="#[message.inboundProperties['http.request.path']]"/>


Using MEL (Mule Expression Language) to read the request from the URL -message.inboundProperties['http.request.path']

3. Print the message in the browser using logger.

<logger doc:name="Log the payload" level="INFO" message="http example #[message.payload]"/>


Execute the Mule application and check the console for the successful deployment.

Enter the below URL in the browser :

http://localhost:7000/testhttp

You will see the response printing in the browser: testhttp

In the above example, we are using two functions.

1. reading from HTTP request and returns to response

2. logs the request

Total Flow XML :

<?xml version="1.0" encoding="UTF-8"?>
<mule version="EE-3.8.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">   
<http:listener-config doc:name="HTTP Listener Configuration" host="0.0.0.0" name="HTTP_Listener_Configuration" port="7000"/>  
  <flow doc:name="HTTP Example" name="EchoFlow">       
    <http:listener config-ref="HTTP_Listener_Configuration" doc:name="Receive HTTP request" path="/*"/>       
    <set-payload doc:name="Set Payload" value="#[message.inboundProperties['http.request.path']]"/>   
    <logger doc:name="Log the payload" level="INFO" message="http example #[message.payload]"/>    
  </flow>
</mule>

That's all!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
mule ,http

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}