Howard Edidin
Howard Edidin

Reputation: 63

How do I parse out the file name in a form-data trigger

From Postman I am doing a Post to my Logic App. I am sending the following:

{
    "headers": {
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Accept": "*/*",
        "Accept-Encoding": "gzip,deflate",
        "Host": "prod-24.centralus.logic.azure.com:443",
        "User-Agent": "PostmanRuntime/6.4.0",
        "Postman-Token": "19018057-41ef-4f96-a3a1-cdbf0a1918bc",
        "Content-Length": "486",
        "Content-Type": "multipart/form-data; boundary=--------------------------117388521639837767242570"
    },
    "body": {
        "$content-type": "multipart/form-data; boundary=--------------------------117388521639837767242570",
        "$content": "LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTExNzM4ODUyMTYzOTgzNzc2NzI0MjU3MA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJzb2Z0d2FyZSI7IGZpbGVuYW1lPSJmaWxlLnppcCINCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vemlwDQoNClBLAwQUAAAACACZVEhLillatwgAAAAGAAAACAAAAGZpbGUudHh0K0ktLslNBQBQSwECHwAUAAAACACZVEhLillatwgAAAAGAAAACAAkAAAAAAAAACAAAAAAAAAAZmlsZS50eHQKACAAAAAAAAEAGADAJqtCS0DTATYTq0JLQNMBlyUeQktA0wFQSwUGAAAAAAEAAQBaAAAALgAAAAAADQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tMTE3Mzg4NTIxNjM5ODM3NzY3MjQyNTcwDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImRldmljZUlkIg0KDQpzMTIzNDUNCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xMTczODg1MjE2Mzk4Mzc3NjcyNDI1NzAtLQ0K",
        "$multipart": [
            {
                "headers": {
                    "Content-Disposition": "form-data; name=\"software\"; filename=\"file.zip\"",
                    "Content-Type": "application/zip"
                },
                "body": {
                    "$content-type": "application/zip",
                    "$content": "UEsDBBQAAAAIAJlUSEuKWVq3CAAAAAYAAAAIAAAAZmlsZS50eHQrSS0uyU0FAFBLAQIfABQAAAAIAJlUSEuKWVq3CAAAAAYAAAAIACQAAAAAAAAAIAAAAAAAAABmaWxlLnR4dAoAIAAAAAAAAQAYAMAmq0JLQNMBNhOrQktA0wGXJR5CS0DTAVBLBQYAAAAAAQABAFoAAAAuAAAAAAA="
                }
            },
            {
                "headers": {
                    "Content-Disposition": "form-data; name=\"deviceId\""
                },
                "body": {
                    "$content-type": "application/octet-stream",
                    "$content": "czEyMzQ1"
                }
            }
        ]
    }
}

I am trying to get filename value and the content of the file. I will be creating a Blob file

Thanks,

Upvotes: 2

Views: 2657

Answers (1)

Joe Eng
Joe Eng

Reputation: 1214

Here's how I'm doing it. I'm assuming you're using the HTTP trigger set to POST. After the HTTP trigger add a Foreach connector, so you can loop through all the files if you're doing a multi file upload.

The Foreach source will be this expression: triggerOutputs().body['$multipart']

Within the foreach you can parse the filename with this ugly expression, which is pretty bad but it works: replace(replace(item().headers['Content-Disposition'], 'form-data; name="myFileUploadInput"; filename="', ''), '"', '')

AFAIK there's no regex support in expressions, which would've been better. You could probably do something with substring, but it'd probably be pretty ugly too.

Here's how you'd get the body within the foreach to save to blob storage: item().body

Upvotes: 4

Related Questions