cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JonathanM
Level: Powered On

HTTP Post multipart/form-data

Hi,

 

I am trying to post SharePoint files to a 3rd party via their API.  Their API uses multipart/form-data.  After some trial and error, I can POSTcontent, but only text files come out correctly on the destination.  Other files (Excel, Word, jpg, etc.) post, but the data is not readable. What do I need to do to the file content to format it properly?

 

Thanks!

 

http post.png

 

17 REPLIES 17
Community Support Team
Community Support Team

Re: HTTP Post multipart/form-data

Hi @JonathanM,

 

The file content is with type base 64 when the files with type Excel, Word, jpg, etc.

 

Best regards,

Alice

JonathanM
Level: Powered On

Re: HTTP Post multipart/form-data

Thanks Alice.  I've tried converting the content a variety of ways, but have not been able to get it converted properly.  What should the formula be to convert the file content?

 

Thanks,

 

Jonathan

Highlighted
SamPo
Level 8

Re: HTTP Post multipart/form-data

@JonathanM Did you ever resolve this issue?

How were you able to post text files using this method in the first place? Everthing I try just converts the whole string to base64 and flow completely ignores the multipart sections.

I'm trying to do it now through swagger but still no luck yet.

Any updates you have here would be much appreciated.

 

Thanks,

Sam

JonathanM
Level: Powered On

Re: HTTP Post multipart/form-data

Hi @SamPo 

 

This worked for me for any type of content (text, Word, Excel, etc.)

2019-04-02_08h15_42.png

SamPo
Level 8

Re: HTTP Post multipart/form-data

@JonathanM Thanks for this.

Unfortunately it's not working for me. I'm posting to the API for Atlassian Jira Cloud.

It responds with a code of 200 but no file is actually created.

 

2019-04-02_1817.png

SamPo
Level 8

Re: HTTP Post multipart/form-data

I got it working in the end. I had to specifically define the content type in the body

Screenshot 2019-04-02 at 22.10.53.png

WillPage
Level 8

Re: HTTP Post multipart/form-data

@JonathanMor @SamPo  Do you know how to modify the request body to include other parameters in the multi-part form? Your examples appear just to have a single part, which is the file itself.

SamPo
Level 8

Re: HTTP Post multipart/form-data

@WillPage  What else are you trying to update?

 

Also are you able to make the call you wish from Postman? I used postman calls to a test flow I built to figure out the structure I needed. Perhaps you can do the same. If you have an example of the postman call then screenshot it here as well if you can.

WillPage
Level 8

Re: HTTP Post multipart/form-data

@SamPo  Thanks for responding.

 

It's a file upload to Bamboo HR. They need a bit of metadata with the file; a category, which is basically the folder it goes in and whether it's shared or not. Here's their doco https://www.bamboohr.com/api/documentation/employees.php#uploadEmployeeFile

 

It works fine in Postman, and it works fine posting plain text in the file content using the example copy-pasted in the documentation (with the category changed).

 

I've tried using your example and adding the metatdata as extra records in the $multipart object like this

 

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"category\""
      },
      "body": {
        "$content": "18"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"fileName\""
      },
      "body": {
        "$content": "test.bmp"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"share\""
      },
      "body": {
        "$content": "yes"
      }
    },
    {
      "headers:": {
        "Content-Disposition": "form-data; name = \"file\"; filename=\"test.bmp\""
      },
      "body": {
        "$content-type": "application/octet-stream",
        "$content": {
          "$content-type": "image/bmp",
          "$content": "iVBORw0KGgoAAA............Jggg=="
        }
      }
    }
  ]
}

However, I get 403 Forbidden back from the server. File category 18 is the only one I have write access to, so there's every chance the metadata isn't being sent properly and it's denying me to write whatever the default file category is.

 

Here's the screen shot from Postman:Capture.PNG

 

WillPage
Level 8

Re: HTTP Post multipart/form-data

What I pasted as the request body there was copied from the output of a failed run. I spotted that Flow is adding its own record for the body... However, it still fails with a 403:

 

Request body as seen in the designer:

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"category\""
      },
      "body": {
        "$content": "18"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"fileName\""
      },
      "body": {
        "$content": "test.bmp"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"share\""
      },
      "body": {
        "$content": "yes"
      }
    },
    {
      "headers:": {
        "Content-Disposition": "form-data; name = \"file\"; filename=\"test.bmp\""
      },
      "body":  @{body('Get_file_content_using_path')}
      
    }
  ]
}

And as seen in the output of a failed run:

 

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"category\""
      },
      "body": {
        "$content": "18"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"fileName\""
      },
      "body": {
        "$content": "test.bmp"
      }
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"share\""
      },
      "body": {
        "$content": "yes"
      }
    },
    {
      "headers:": {
        "Content-Disposition": "form-data; name = \"file\"; filename=\"test.bmp\""
      },
      "body": {
        "$content-type": "image/bmp",
        "$content": "iVBORw.................Jggg=="
      }
    }
  ]
}
SamPo
Level 8

Re: HTTP Post multipart/form-data

@WillPage  Sorry for the late reply.

 

1) Does your postman flow work?

2) Assuming it does work, change the endpoint of the postman flow to a new flow just with an HTTP request trigger (add just a compose or something so you can save it. Then run it once and open the log of the flow that ran then copy the body received by the HTTP trigger here. If one of the items youve posted above is already this then let me know which one.

 

Sam

WillPage
Level 8

Re: HTTP Post multipart/form-data

@SamPoNow that I've done that it's clear why it isn't working.

 

This is the body of the request coming from Postman, as captured by a flow with HTTP trigger:

 

----------------------------633613363255592221149085
Content-Disposition: form-data; name="category"

18
----------------------------633613363255592221149085
Content-Disposition: form-data; name="fileName"

Will Page - Signed Induction.pdf
----------------------------633613363255592221149085
Content-Disposition: form-data; name="share"

yes
----------------------------633613363255592221149085
Content-Disposition: form-data; name="file"; filename="Will Page - Signed Induction.pdf"
Content-Type: application/pdf

%PDF-1.7
... Rest of PDF file.

 

 

..and this is from Flow:

 

--cbd9b14e-6955-43e3-a757-c4e5d1d3d3c3
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="category"

{"$content":"18"}
--cbd9b14e-6955-43e3-a757-c4e5d1d3d3c3
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="fileName"

{"$content":"Will Page - Signed Induction.pdf"}
--cbd9b14e-6955-43e3-a757-c4e5d1d3d3c3
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="share"

{"$content":"yes"}
--cbd9b14e-6955-43e3-a757-c4e5d1d3d3c3
content-type: application/pdf

%PDF-1.7
... Rest of PDF file

 

 

I do note that the file content is not corrupt. It looks the same as the original in a text editor at least, which is more than can be said when adding dynamic content from a Get file content action directly into a plain text request body, (like the output from Postman above or the example on the Bamboo HR documentation).

 

Now, if I can define the multipart boundaries using JSON, and the file content but have the metadata as plain text this would work. Is that possible, or am I going to have to do this in an Azure Function?

WillPage
Level 8

Re: HTTP Post multipart/form-data

So, this body:

 

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"category\""
      },
      "body": "18"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"fileName\""
      },
      "body": "Will Page - Signed Induction.pdf"
  
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"share\""
      },
      "body": "yes"
    },
    {
      "headers:": {
        "Content-Disposition": "form-data; name=\"file\"; filename=\"Will Page - Signed Induction.pdf\""
      },
      "body": @{body('Get_file_content')}
    }
  ]
}

Gives me this output:

--3b77c044-c94c-4156-8322-01ca7e812bcb
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="category"

18
--3b77c044-c94c-4156-8322-01ca7e812bcb
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="fileName"

Will Page - Signed Induction.pdf
--3b77c044-c94c-4156-8322-01ca7e812bcb
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="share"

yes
--3b77c044-c94c-4156-8322-01ca7e812bcb
content-type: application/pdf

%PDF-1.7

...which looks OK apart from the Content-Type header I didn't ask for in each section. Unfortunately though, The Bamboo HR API gives me a 400 error when I send it this. Guess I'm stuck!

WillPage
Level 8

Re: HTTP Post multipart/form-data

This is the error coming back from the Bamboo HR API now....

Capture.PNG

WillPage
Level 8

Re: HTTP Post multipart/form-data

It's just clicked. For some reason the last part:

 

 {
      "headers:": {
        "Content-Disposition": "form-data; name=\"file\"; filename=\"Will Page - Signed Induction.pdf\"",
"Content-Type": "application/pdf"
      },
      "body": @{body('Get_file_content')}
    }

When run through the Flow comes out without the Content-Disposition attached:

--4a6c27da-af8b-479d-af08-824f21977c40
content-type: application/pdf

%PDF-1.7
..Rest of PDF

--4a6c27da-af8b-479d-af08-824f21977c40

..so close but not quite there!

WillPage
Level 8

Re: HTTP Post multipart/form-data

@SamPoThanks so much for your help. I finally figured this out:

 

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"category\""
      },
      "body": "18"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"fileName\""
      },
      "body": "Will Page - Signed Induction.pdf"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"share\""
      },
      "body": "yes"
    },
    {
      "headers": {
"Content-Disposition": "form-data; name=\"file\"; filename=\"Will Page - Signed Induction.pdf\""},
      "body": {
"$content-type": "application/pdf",
"$content": "@{base64(body('Get_file_content'))}"
}
    }
  ]
}

Without your tip of creating a Flow to capture and debug the output I'd have spent a million years on this - having that info was crucial.

 

The last part was simply to convert the output of Get file content to Base64 rather than feed the raw content straight into the Flow and let it infer the content type and create an object out of it.

 

So happy to see this working!

SamPo
Level 8

Re: HTTP Post multipart/form-data

@WillPage So happy to see you've got it working! 

Glad to be of service.

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020.

thirdimage

Flow Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 370 members 5,578 guests
Please welcome our newest community members: