How can i get headers from the request of an HTTP Listener?

0 votes
asked May 1 by rich-c-2789 (17,430 points)
I need to access the headers that were sent in the request to an HTTP Listener.

1 Answer

0 votes

You can access a requests headers if the "Extract content as message (discard HTTP Headers and other MetaData)" is not checked on the "HTTP Configuration" tab of the "HTTP Listener".  When it is not checked, you can access request headers within your mapping and condition scripts.

An HTTP request is represented in XML. The headers are located within the <Headers> element, which contains individual headers as separate nested elements. To access a header or the nested header elements within your mapping scripts use the following code:

//To access a headers value, access them by name
var connection = source.getNode('/Request/Headers/Connection');"Connection: " + connection);

var userAgent = source.getNode('/Request/Headers/User-Agent');"User-Agent: " + userAgent);

var host = source.getNode('/Request/Headers/Host');"Host: " + host);

var acceptEncoding = source.getNode('/Request/Headers/Accept-Encoding');"Accept-Encoding: " + acceptEncoding);

var contentType = source.getNode('/Request/Headers/Content-Type');"Content-Type: " + contentType);

//This returns the xml element Headers with the nested named header elements
var headers = source.getNode('/Request/Headers');"Headers: " + headers);

The above code accesses individual headers by name as well as returning an XML snippet of the header element with all the nested named header elements. The later may only be useful while debugging or developing a channel to determine what headers may have been sent in a request. The results after running the above code may look something like this:

Connection: keep-alive
User-Agent: Apache-HttpClient/5.3 (Java/17.0.4)
Host: localhost:8083
Accept-Encoding: gzip, x-gzip, deflate
Content-Type: application/xml
Headers: <Headers>
<User-Agent><![CDATA[Apache-HttpClient/5.3 (Java/17.0.4)]]></User-Agent>
<Accept-Encoding><![CDATA[gzip, x-gzip, deflate]]></Accept-Encoding>


See also:

Handling HTTP requests and responses

Processing Requests in mapping nodes via HTTP Listener source node


answered May 1 by rich-c-2789 (17,430 points)
edited May 16 by rich-c-2789