简体   繁体   中英

Azure Logic Apps: Change HTTP request body via condition

I am currently developing an Azure Logic App that imports data by repeatedly performing HTTP POST requests. My backend has a property that may not be null or empty and because I don't want to change this I tried not including the property in the request body at all.

So for example

{
    "id": "1234-5678",
    "value": @{variables('myId')}
} 

shall only be included in the body if the variable is neither null or empty. I tried adding an expression similar to

if(not(variables('myId')), '', concat(',{"id":"1234-5678","value": ', variables('myId'), '},'))

To the request body to make it more dynamic but now it says that the body is not valid anymore (invalid json). Is there anything else I could use? Using a condition and multiple different HTTP requests not not an option because I have multiple values that need this treatment.

Yep, add it first and then remove it using a condition around the removeProperty expression. You can also use the addProperty approach but I went for the former. It really doesn't matter.

Load this definition into your tenant and you'll see it working.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Initialize_MyId": {
                "inputs": {
                    "variables": [
                        {
                            "name": "MyId",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_New_Payload": {
                "inputs": {
                    "variables": [
                        {
                            "name": "New Payload",
                            "type": "object",
                            "value": "@if(equals(variables('MyId'), ''), removeProperty(variables('Payload'), 'value'), variables('Payload'))"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Payload": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Payload": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Payload",
                            "type": "object",
                            "value": {
                                "id": "1234-5678",
                                "value": "@{variables('myId')}"
                            }
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_MyId": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

As a basic example, I have three steps...

  1. Create a variable that stores a value for MyId
  2. Create a variable of type Object that stores the payload you spoke about in your question.
  3. Use a condition to remove the value property if the value of MyId is blank.

The specific expression in question is...

if(equals(variables('MyId'), ''), removeProperty(variables('Payload'), 'value'), variables('Payload'))

流

Result Blank

结果空白

Result NOT Blank

结果不为空

I chose to solve this by sending multiple requests, first one for all properties that were already working and then one for each of those "special" properties. I know it will severely hurt the application's performance but as time became more of a concern I went with this anyways.

First, I create an object in the database with a POST request, parse it to JSON with the respective function, get the object id and then perform multiple PUT requests on it depending on which values I have. If my value exists I only add one property to the object and if the value does not exist I don't perform a request at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM