HTTP Redirection or Forwarding using Mule App

This document covers a Mule Application code that is capable to taking some input parameters or payload and after some kind of processing, it can redirect or forward the flow to another URL. Such implementation is made possible with HTTP Listener and can be useful in a variety of use-cases.

Let’s begin...

Pre-requisites

  • Postman
  • Anypoint Studio

Anypoint Studio Code

Create a new Mule Application project in Anypoint Studio. Open the project’s main *.xml file and use the following XML code to setup a listener flow -

<flow 
  name="http-RedirectFlow" 
  doc:id="0a946780-95b5-4dea-a0db-37efe5f3f36b" 
>
  <http:listener 
    doc:name="/request" 
    doc:id="77f08c1c-da16-4f7e-b591-385cfd036593" 
    config-ref="HTTP_Listener_config" 
    path="/request"
  >
    <http:response
      statusCode='#["302"]' 
    >
      <http:headers >
        <![CDATA[#[output application/java
        ---
        {
          "Location" : "https://www.loveleshkalonia.com"
        }]]]>
      </http:headers>
    </http:response>
    <http:error-response >
      <http:body >
        <![CDATA[#[output text/plain
        ---
        error.description]]]>
      </http:body>
    </http:error-response>
  </http:listener>
  <logger level="INFO" 
    doc:name="Mulesoft Transformation Starts" 
    doc:id="089255c4-9e26-4e07-b01d-6c91d4c77436" 
    message="######## Mulesoft Transformation Starts ########"
  />
  <logger level="INFO" 
    doc:name="MetaData" 
    doc:id="5d865653-65d9-405b-a067-710df42ad157" 
  />
  <logger level="INFO" 
    doc:name="Mulesoft Transformation Ends" 
    doc:id="2545c0a5-06d2-4c81-bbd8-7e2c9d9caf33" 
    message="######## Mulesoft Transformation Ends ########" 
  />
</flow>

This code will look like this -

Listener Flow

You will have to create a Listener configuration too. For that, you can use the following code -

<http:listener-config
  name="HTTP_Listener_config"
  doc:name="HTTP Listener config"
  doc:id="5f29422b-31e7-4094-9c9d-5088b6ba2c78"
>
  <http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>

You may have already noticed the place where http status code and the redirect URL is being configured. In the GUI you can find them in the Listener Properties > Responses tab as shown -

Redirection | Listener Properties

Run the Mule Application now.

It would be best to test this in a Browser. So just open http://localhost:8081 in your Browser and you'll see that it will redirect you to my website https://www.loveleshkalonia.com.

This completes the demo.

Troubleshooting Common Issues

I don’t think anyone will encounter any issues using this guide so I will leave the common issues part blank this time :)

Comments