Search code examples
azure-logic-apps

Handling Success and Error API responses in Logic Apps workflow


I am bulding a Logic Apps workflow and I need to handle 2 different type of responses returned back by a 3rd API service by using an if condition.

The API response in case of the API being able to process the data correctly looks pretty much like this:

{ "Key": “AA11-AA11-AA11-AA11", "Geocode":true, "Addresses": [ { "Address":"", "Address1":"Sageweg 27", "Address2":"Neuenburg", "Address3":"", "Address4":"" }, { "Address":"", "Address1":"Schubartstr. 111", "Address2":"Bietigheim-Bissingen", "Address3":"74321", "Address4":"" } ] }

while in case of "error" looks like this:

{ "Number": 18, "Description": "Missing or invalid parameters", "Cause": "A required parameter was not supplied of the value of a parameter cannnot be converted into the right type.", "Resolution": "Check the parameters passed and their values against the specification for this service." }

where 18 is just of the possible error scenarios.

How can I set the workflow in terms of actions, and consequently the If condition to discriminate between success and "error" response?

This is what I have so far.

enter image description here

I've tried to build 2 Parse JSON actions, manaing the success and error paylods scenarios, in parallel, but the workflow takes ages in running, and I also found it tricky to manage the fact that in the success scenario the response is an array, while in the error it's an object, and also not all fields are optional, so this solution looked not the cleanest one to me.

Thanks in advance!


Solution

  • You can handle the success or failure responses as shown below-

    enter image description here

    • For testing purpose, I am sending both success and failure payload manually while invoking the workflow.
    • Here I have generated a combined schema for both success and failed responses which is being used in Parse JSON action.

    enter image description here

    {
        "properties": {
            "Addresses": {
                "items": {
                    "properties": {
                        "Address": {
                            "type": "string"
                        },
                        "Address1": {
                            "type": "string"
                        },
                        "Address2": {
                            "type": "string"
                        },
                        "Address3": {
                            "type": "string"
                        },
                        "Address4": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "type": "array"
            },
            "Cause": {
                "type": "string"
            },
            "Description": {
                "type": "string"
            },
            "Geocode": {
                "type": "boolean"
            },
            "Key": {
                "type": "string"
            },
            "Number": {
                "type": "integer"
            },
            "Resolution": {
                "type": "string"
            }
        },
        "type": "object"
    }
    
    • In Condition action, I am verifying whether Key property is null or not. You can use any property which is unique in success and failure payload.

    enter image description here

    • With this configuration, I am able to get the expected response.

    Success-

    enter image description here

    Failure-

    enter image description here