DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Mule-HTTP Request and Response Example

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.

Rajesh Kumar user avatar by
Rajesh Kumar
·
Jul. 05, 16 · Web Dev Zone · Tutorial
Like (5)
Save
Tweet
28.33K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Requests

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • API Security Weekly: Issue 173
  • How to Migrate to the Open-Source Cadence Workflow
  • Key Metrics and Measurements to Track Project and Product Performance
  • Kubernetes Data Simplicity: Getting Started With K8ssandra

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo