Mule Recipe: Dynamically set Mule HTTP Request path

Share this:

With the HTTP Request connector on Mule, you will notice that you cannot use Mule Expression Language (MEL) on the Path configurations. The connector evaluates that config literally and would assume /#[payload] as is and make a HTTP request at http://api.domain.com/#[payload]

But fret not. There is indeed a way to set the HTTP Request’s path dynamically. Instead of trying to dynamically set the path in the http:request path config, you set it at the http:request-config basePath instead!

Dynamically set Mule HTTP Request path

The following code is an example where I have two flows to demonstrate how this works. The first init flow is the one where the HTTP request is made with a dynamic path. The second flow is a simple hello world flow that logs the name that is passed in the request uri.

So, when you hit the init flow, it will create a variable called apiPath with the value that is in the payload (mocked in this example code) and make a request out to http://0.0.0.0:8081/hello/<apiPathVariableValue>. As mentioned, the trick is to put the Mule expression in the basePath configuration instead of the path configuration. When the HTTP request connector executes, it references the http:request-config and evaluates the basePath based on the MEL expression there. In the example below, it’s #[flowVars.apiPath].

For the HTTP request config, simple leave / as the path.

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="0.0.0.0" port="8081" basePath="#[flowVars.apiPath]" doc:name="HTTP Request Configuration"/>
<flow name="init">
  <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="bootstrap"/>
  <set-payload value="John Doe" doc:name="Set the payload with a name"/>
  <set-variable 
    variableName="apiPath" 
    value="/hello/#[payload]" 
    doc:name="Set site address variable"/>
  <http:request config-ref="HTTP_Request_Configuration" path="/" method="GET" doc:name="sayHello"/>
</flow>
<flow name="say_hello">
  <http:listener config-ref="HTTP_Listener_Configuration" path="/hello/{name}" doc:name="helloListener"/>
  <logger message="#[message.inboundProperties.'http.uri.params'.name]" level="INFO" doc:name="Logger"/>
</flow>\

The example flow is shown below.

mule-dynamic-http-request-path-flow

The Dynamic Http Request flow

If you execute the flow and hit http://0.0.0.0:8081/ you should see the following in the logs.

INFO  2017-08-17 13:08:56,291 [[http_req_mel].HTTP_Listener_Configuration.worker.02] org.mule.api.processor.LoggerMessageProcessor: John Doe

Hope this quick tip is useful if you need to dynamically set HTTP Request path in your Mule project use case.

Share this:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.