Search code examples
azureazure-logic-apps

Azure Logic App - How to create file to Sharepoint using HTTP trigger?


  1. I created a Logic App to trigger to HTTP requests and where in request I send a pdf file inside multipart/form-data and where file content is binary form as specified by application/octet-stream. Below is the actual Python code I used to send the HTTP request.
        file_name = os.path.basename(file_path)
        with open(file_path, 'rb') as file:
            file_content = file.read()

        files = {
            'file_content': (file_name, file_content, 'application/octet-stream')
        }

        response = requests.post(logic_app_url, files=files)
  1. In Logic app, the HTTP request gets triggered and data is received as expected (that I can see). After trigger, I use Sharepoint connector and "Create File" action to create a file. For getting the content of the PDF file from HTTP request, I use the following expression
base64ToBinary(triggerBody()?['$multipart']?[0]?['body']?['$content'])
  1. The file is created to the Sharepoint, however the data is corrupted in the created PDF file.
  • The PDF opens but it is blank.
  • When I inspect the created PDF file, some characters are not correct when comparing to original PDF file.

Additional Information:

Here is the raw output of body from the HTTP trigger (before the Sharepoint connector)

    "body": {
        "$content-type": "multipart/form-data; boundary=17d2fbd65b268c5f0383c18eaeafbfea",
        "$content": "LS0xN2QyZmJkNjViMjY4YzVmMDM4M2MxOGVhZWFmYmZlYQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlX2NvbnRlbnQiOyBmaWx.....YzE4ZWFlYWZiZmVhLS0NCg==",
        "$multipart": [
            {
                "headers": {
                    "Content-Disposition": "form-data; name=\"file_content\"; filename=\"original_pdf.pdf\"",
                    "Content-Type": "application/octet-stream"
                },
                "body": {
                    "$content-type": "application/octet-stream",
                    "$content": "JVBERi0xLjQKJazcIKu6CjEgMCBvYmoKPDwgL1BhZ2VzIDIgMCBSIC9UeXBlIC9DYXRhbG9nID4......CnN0YXJ0eHJlZgoxNTkyMgolJUVPRgo="
                }
            }
        ]
    }

The file content is correctly base64 encoded (in above it is only partly given). I get exactly the same content if I create base64 encoded string from the file and save it in my Python function to a file locally and compare to content received by the Azure Logic App.

Any ideas would be helpful.


Solution

  • Here you don't need to convert to binary:

    base64ToBinary(triggerBody()?['$multipart']?[0]?['body']?['$content'])
    

    Design which worked for me:

    enter image description here

    As you created a function app, which also interconnected with storage account, I would suggest you to first send file to storage account and then get it . And there is no need to convert it to binary.

    My pdf content looks same as yours, and i have just used it as it is and it worked:

    enter image description here

    Output:

    enter image description here

    File got created in sharepoint:

    enter image description here

    There is nothing wrong ion Logic app workflow, you need to just use its content, not convert it.