Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

HTTP Connector XML Reference

The HTTP connector listens for events or performs operations based on the HTTP or HTTPS protocol. Using the HTTPS protocol requires a TLS context child element in the configuration as shown in the next section. Mule 4.0 and later supports DataWeave syntax for accessing Listener and Request attributes. Mule continues to support the Mule Expression Language (MEL). Tables in this document map one syntax to the other.

Listener

The listener-config parent element organizes connection elements, such as the listen port and tls:context, inside a child listener-connection element. The connection element is a child of http:listener-config, as shown in the following code.


         
      
1
2
3
4
5
6
7
<http:listener-config name="lisConfig" basePath="api">
  <http:listener-connection protocol="https" port="${serverPort}" host="localhost" usePersistentConnections="true">
    <tls:context>
      <tls:key-store path="keystore.jks" keyPassword="key" password="pass"/>
    </tls:context>
  </http:listener-connection>
</http:listener-config>

The following code places an HTTP Listener in a flow:


         
      
1
2
3
4
<flow name="server">
  <http:listener path="test" allowedMethods="GET" config-ref="lisConfig"/>
  <logger message="HEY"/>
</flow>

To access HTTP Listener attributes, use DataWeave syntax as shown in the following table.

HTTP object 3.x 4.x

Method

#[inboundProperties.’http.method’]

#[attributes.method]

Path

#[inboundProperties.’http.listener.path’]

#[attributes.listenerPath]

Relative Path

#[inboundProperties.’http.relative.path’]

#[attributes.relativePath]

Request URI

#[inboundProperties.’http.request.uri’]

#[attributes.requestUri]

Query String

#[inboundProperties.’http.query.string’]

#[attributes.queryString]

Query Parameters

#[inboundProperties.’http.query.params’]

#[attributes.queryParams]

URI Parameters

#[inboundProperties.’http.uri.params’]

#[attributes.uriParams]

Version

#[inboundProperties.’http.version’]

#[attributes.version]

Scheme

#[inboundProperties.’http.scheme’]

#[attributes.scheme]

Headers

#[inboundProperties]

#[attributes.headers]

Remote Address

#[inboundProperties.’http.remote.address’]

#[attributes.remoteAddress]

Client Certificate

#[inboundProperties.’http.client.cert’]

#[attributes.clientCertificate]

HTTP Request

Consistent with the listener, the request-config parent element organizes connection elements of the request inside a child request-connection element, as shown in the following code.


         
      
1
2
3
4
5
6
7
8
9
10
<http:request-config name="reqConfig" basePath="api/v2">
  <http:request-connection protocol="HTTP" port="${clientPort}" host="localhost">
    <tls:context>
      <tls:trust-store path="ssltest-cacerts.jks" password="changeit"/>
    </tls:context>
    <http:authentication>
      <http:basic-authentication username="#[user]" password="#[password]" preemptive="#[preemptive]" />
    </http:authentication>
  </http:request-connection>
</http:requester-config>

The following code places an HTTP request in a flow. The HTTP request returns HttpResponseAttributes.


         
      
1
2
3
<flow name="client">
   <http:request path="/" config-ref="reqConfig"/>
</flow>

To access HTTP request attributes, use DataWeave syntax as shown in the following table:

HTTP object 3.x 4.x

Status Code

#[inboundProperties.’http.status]

#[attributes.statusCode]

Reason Phrase

#[inboundProperties.’http.reason’]

#[attributes.reasonPhrase]

Headers

#[inboundProperties]

#[attributes.headers]

HTTP Request, Response, and Error Response Body

Defining the body of an HTTP request, response, and error response is optional. When you do not explicitly define the body element, the request or response returns the default payload.

The following example request defines a custom body, a key=value query parameter, an X-Custom header having custom as a value. The uriParamsMap variable populates the URI parameter uriParam.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<http:request path="/{uriParam}/test"  config-ref="reqConfig">
   <http:body>
       #['Custom body']
   </http:body>
   <http:headers>
       #[{'X-Custom' : 'custom'}]
   </http:headers>
   <http:query-params>
       #[{'key’' : 'value’'}]
   </http:query-params>
   <http:uri-params>
       #[uriParamsMap]
   </http:uri-params>
</http:request>

The following code lacks an explicity defined body element. The returned value is the default payload. The payload defines a response to a successful execution that consists of a 200 status code and an Ok reason phrase with headers Name, with content as value, and with Something having a value of the someValue.


         
      
1
2
3
4
5
6
7
<http:listener path="test" allowedMethods="GET" config-ref="lisConfig">
   <http:response statusCode="200" reasonPhrase="Ok">
       <http:headers>
           #[{"Name" : "content", "Something" : someValue}]
       </http:headers>
   </http:response>
</http:listener>

The following code defines a response to an execution failure that returns a 502 status code and a Failure reason phrase with the header Error with FIRE as the value and Oops, something went terribly wrong as the body.


         
      
1
2
3
4
5
6
7
8
9
10
<http:listener path="test" allowedMethods="GET" config-ref="lisConfig">
   <http:error-response statusCode="502" reasonPhrase="Failure">
       <http:body>
           #[‘Oops, something went terribly wrong.’]
       </http:body>
       <http:headers>
           #[{"Error" : "FIRE"}]
       </http:headers>
   </http:error-response>
</http:listener>

HTTP Operations Reference

The following code shows the syntax of the load static resource operation:


         
      
1
2
3
4
<flow name="server">
   <http:listener path="test" allowedMethods="GET" config-ref="lisConfig"/>
   <http:load-static-resource resourceBasePath="root" defaultFile="index.html"/>
</flow>

The following example shows the syntax of the basic security filter operation:


         
      
1
2
3
4
5
<flow name="listenerBasicAuth">
   <http:listener config-ref="listenerConfigBasicAuth" path="/basic"/>
   <http:basic-security-filter realm="mule-realm"/>
   <logger message="TestBasicAuthOk"/>
</flow>