RuSm
RuSm

Reputation: 37

How to iterate over the headers in an Azure Logic App?

In a http triggered Logic App I'd like to iterate over the incoming http headers and use only the x-... headers. These headers should be appended to a JSON message.

I know how to access the headers using triggerOutputs()['headers'], how to iterate over collections and how to construct a JSON message using compose. But how to iterate over the headers?

Note: I don't know the x-... header names and I'd like to append any x-... header into the message, I don't want to update the Logic App if a new header is being added or one is removed.

I tried to create a ForEach that iterates over triggerOutputs()['headers'], but that's not a collection.

The header content:

{
  "Cache-Control": "no-cache",
  "Connection": "keep-alive",
  "Accept": "*/*",
  "Accept-Encoding": "br,gzip,deflate",
  "Host": "prod-42.westeurope.logic.azure.com:443",
  "x-batch-uuid": "e74f68b9-d69e-4d50-bfe2-65bd6b38dd45",
  "x-event": "12361",
  "x-sequence": "1",
  "x-trigger-id": "sample-trigger-id",
  "x-trigger-time": "1672935303",
  "x-uuid": "66fa9513-ab42-4f31-90ff-b44582f7d72f",
  "Content-Length": "308",
  "Content-Type": "application/json"
}

Current version with static x-header copy action

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "MessageBody": {
                "inputs": {
                    "data": "@triggerBody()",
                    "meta": "@outputs('X-Headers')"
                },
                "runAfter": {
                    "X-Headers": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Send_message": {
                "inputs": {
                    "body": {
                        "ContentData": "@{base64(outputs('MessageBody'))}"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['servicebus']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/@{encodeURIComponent(encodeURIComponent('schedule-events'))}/messages"
                },
                "runAfter": {
                    "MessageBody": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "X-Headers": {
                "inputs": {
                    "x-batch-uuid": "@{triggerOutputs()?['headers']?['x-batch-uuid']}",
                    "x-event": "@{triggerOutputs()?['headers']?['x-event']}",
                    "x-sequence": "@{triggerOutputs()?['headers']?['x-sequence']}",
                    "x-trigger-id": "@{triggerOutputs()?['headers']?['x-trigger-id']}",
                    "x-trigger-time": "@{triggerOutputs()?['headers']?['x-trigger-time']}",
                    "x-uuid": "@{triggerOutputs()?['headers']?['x-uuid']}"
                },
                "runAfter": {},
                "type": "Compose"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "person": {
                                "properties": {
                                    "href": {
                                        "type": "string"
                                    },
                                    "personId": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "servicebus": {
                    "connectionId": "/subscriptions/.../resourceGroups/.../providers/Microsoft.Web/connections/servicebus-1",
                    "connectionName": "servicebus-1",
                    "id": "/subscriptions/.../providers/Microsoft.Web/locations/westeurope/managedApis/servicebus"
                }
            }
        }
    }
}

Upvotes: 2

Views: 521

Answers (1)

vijaya
vijaya

Reputation: 1721

As mentioned by @skin, you cannot iterate for each loop over an object. Headers is an object, and we can iterate for each loop for array only. As a workaround I have tried a below method. Kindly try if it helps in your case.

  1. Created logic app as shown below, enter image description here
  2. In for each loop, taken condition as shown below if(startsWith(item(),'-x'),true,false) enter image description here
  3. Logic app ran successfully as shown below, enter image description here

Upvotes: 2

Related Questions