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

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

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!

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

Topics:
mule ,http

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}