Recently, I read a question about a Logic App that received an HTTP request. The purpose of the Logic App was to add some extra HTTP headers to the request, while retaining the orignal ones, before forwarding the message to another HTTP endpoint. This short blog demonstrates how you can enrich HTTP headers, without losing the orignal HTTP headers.
The solution
Our Logic App consists of three actions:
The first shape is an Request trigger that receives the original request. Default configuration.
The second one is an Initialize Variable that constructs the new HTTP Headers object.
The third action forwards the original request to a RequestBin endpoint. Notice that the Headers parameter accepts both key-value pairs or JSON objects. I use the following expression to merge the new with the orignal HTTP headers:
@union(triggerOutputs().Headers, variables('NewHeaders'))
Edit: I received the question if this can be done without an intermediate Initialize Variable action. Yes, it’s possible. A little cheaper, but a little less readible:
union(triggerOutputs().Headers, json('{ "x-new-header-3": "header-3", "x-new-header-4": "header-4"}'))
Testing
Let’s send an HTTP request to the Logic App, with 2 original HTTP headers.
If we now inspect the received request in RequestBin, we see that the two new HTTP headers are appended to the original ones. Additionally, some default Logic Apps HTTP headers are added to the request too.
Conclusion
By using the @union workflow function, we are able to easily append additional values to the incoming HTTP headers.
Hope this was a useful one!
Toon