Nathan
Nathan

Reputation: 21

APIM and Logic App to read a file via REST API

I am new to the Azure or cloud world. I am trying to set up a pet project using APIM and Logic App. I want to submit a file via APIM and use Logic App to process the file contents. I have created the swagger file for APIM below. How should the HTTP Trigger "Request JSON schema" in my Logic App look like in order to process this file. The file will be text/plain. Any help would be great. Here is part of the swagger definition.

"paths": {
  "/submitEmployeeForm": {
    "post": {
      "description": "This Operation is used to submit employee files.",
      "operationId": "submitEmployee",
      "summary": "submitEmployeeDetails",
      "responses": {
        "200": {
          "description": "Accepted",
          "schema": {
            "$ref": "#/definitions/employeeResponse"
          }
        },
        "400": {
          "description": "400 Bad request",
          "schema": {
            "$ref": "#/definitions/empReqError"
          }
        },
        "401": {
          "description":  "Authorization Required"
        },
        "403": {
          "description": "Forbidden"
        },
        "404": {
          "description": "Not Found"
      },
      "500": {
        "description": "Internal Server Error"
       }          
    },
    "consumes": [
      "multipart/form-data"
    ],
    "produces": [
      "application/json"
    ],
    "parameters": [
      {
        "name": "transactionId",
        "in": "header",
        "description": "Unique id to track requests.",
        "type": "string",
        "format": "uuid",
        "required": true
      },
      {
        "in": "formData",
        "name": "employeefile",
        "description": "upload employee file for processing",
        "required": true,
        "type": "file"
      }
    ]
  }
}

Upvotes: 1

Views: 733

Answers (1)

10p
10p

Reputation: 6706

  1. "How should the HTTP Trigger "Request Body JSON Schema" in my Logic App look like in order to process this file? The file will be text/plain."

First, trigger your Logic App without specifying any schema and check the output of the Logic App trigger in the Logic App run history.

If you send a single file, it will look similar to this:

{
  "$content-type": "multipart/form-data; boundary=--------------------------708151345350107134405173",
  "$content": "LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTcwODE1MTM0NTM1MDEwNzEzNDQwNTE3Mw0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSIiOyBmaWxlbmFtZT0idGVzdC50eHQiDQpDb250ZW50LVR5cGU6IHRleHQvcGxhaW4NCg0KVGVzdA0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTcwODE1MTM0NTM1MDEwNzEzNDQwNTE3My0tDQo=",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"\"; filename=\"test.txt\"",
        "Content-Type": "text/plain"
      },
      "body": "Test"
    }
  ]
}

Copy it to the clipboard.

In the Logic App designer, click the Use sample payload to generate schema link in the trigger and paste the copied JSON. Click Done. It will generate the schema automatically:

{
    "type": "object",
    "properties": {
        "$content-type": {
            "type": "string"
        },
        "$content": {
            "type": "string"
        },
        "$multipart": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "headers": {
                        "type": "object",
                        "properties": {
                            "Content-Disposition": {
                                "type": "string"
                            },
                            "Content-Type": {
                                "type": "string"
                            }
                        }
                    },
                    "body": {
                        "type": "string"
                    }
                },
                "required": [
                    "headers",
                    "body"
                ]
            }
        }
    }
}
  1. Your real question is probably "how can I process the file contents in my Logic App?"

To do this, you can use the following Expression (not Dynamic content) in your Logic App actions: triggerBody()?['$multipart']?[0]?['body']. As $multipart is an array, first you take its first element ([0]), and then you get the contents of the body element (the file contents). Strictly speaking, you don't even need to specify the Request Body JSON Schema for this to work. The schema is needed only if you need to use a Dynamic content.

Upvotes: 1

Related Questions