# Uploading a file using Power Automate

**URL:** <https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, professional\
**Created:** [August 9, 2023, 3:19pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129 "2023-08-09T15:19:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKhan786](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@AKhan786](https://community.hubspot.com/u/AKhan786)\
**Post date:** [August 9, 2023, 3:19pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/1 "2023-08-09T15:19:59Z")

</div>

I have files stored in a SharePoint library, and want to use Power Automate ¶ to upload them to HubSpot. I can’t use the async import from URL API as these would be sensitive files.

Ideally I’d like to use the new API ([CMS API | Files (hubspot.com)](https://developers.hubspot.com/docs/api/files/files)) **POST /files/v3/files** instead of the legacy one [Upload a new file | COS Files API (hubspot.com)](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file) but after trying so much, at this point, either would be fine.

I’m unsure how to provide the file information for the API calls, and keep getting:

```lia-code-sample
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Reason:
<pre> Unsupported Media Type</pre></p>
</body>
</html>

```

My HTTP request (with the legacy API) in PA looks like this:

 ![AKhan786_2-1691593396023.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/3/32c69a02aba4efc0781070f382a8a4e966c69bc0.png)

The file is the _$content_ output of the **Get File Content** block, which outputs the content type and content like so:

 ![AKhan786_1-1691593240284.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/c/c0ad93b60e166a59380bdffb554414cab8bd5751.png)

I’ve checked all the other posts I could find with people experiencing similar issues and can’t seem to get it to work…One person posted their Powershell code and even replicating that resulted in the same error:

```lia-code-sample
clear
$apiKey = 'Bearer **key_here**'
$uri = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=$apiKey"

$fileOptions = @{
 access = 'PRIVATE'
 overwrite = 'true'
 duplicateValidationStrategy = 'NONE'
 duplicateValidationScope = 'EXACT_FOLDER'
}

$body = @{
 fileName = 'testfile.txt'
 file = Get-Item C:\temp\testfile.txt
 folderPath = '/Uploaded Documents'
 charsetHunch = 'UTF-8'
 options = $fileOptions | ConvertTo-Json
}

$response = Invoke-WebRequest -Uri $uri -Method Post -Body $body -Verbose 
$response.results

```

Is it the charsetHunch that I’m not setting properly? I’m stumped…and I must say, the documentation is relly not helpfull at all [CMS API | Files (hubspot.com)](https://developers.hubspot.com/docs/api/files/files) No example is provided, doesn’t say which params are necessary for the upload file endpoint, and isn’t sufficiently explanatory at all 😣

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [August 9, 2023, 5:26pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/2 "2023-08-09T17:26:29Z")

</div>

Hey @AKhan786,

I think the issue is to do with the “Content-Type” header. Regardless of whether you are using the legacy endpoint or the newer endpoint the correct header to use is “multipart/form-data” vs “application/pdf” which you seem to be referencing.

---

<div class="post-metadata">

**Author:** ![AKhan786](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@AKhan786](https://community.hubspot.com/u/AKhan786)\
**Post date:** [August 10, 2023, 1:45pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/3 "2023-08-10T13:45:27Z")

</div>

I’ve tried that header as-well, but I got Bad Request HTTP 400 error:

```lia-code-sample
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Reason:
<pre> Bad Request</pre></p>
</body>
</html>

```

For reference, my HTTP request inputs look like this:

```lia-code-sample
{
 "uri": "https://api.hubapi.com/files/v3/files",
 "method": "POST",
 "headers": {
 "Content-Type": "multipart/form-data"
 },
 "retryPolicy": {
 "type": "Fixed",
 "interval": "PT5S",
 "count": 5
 },
 "authentication": {
 "value": "*sanitized*",
 "type": "Raw"
 },
 "body": {
 "file": "*really looooong jumble of text here from "Get Item" block in PA*",
 "fileName": "SoWTest.xlsx",
 "folderPath": "/Uploaded Documents",
 "charsetHunch": "UTF-8",
 "options": {
 "access": "PRIVATE",
 "duplicateValidationStrategy": "NONE",
 "duplicateValidationScope": "EXACT_FOLDER",
 "overwrite": true
 }
 }
}

```

---

<div class="post-metadata">

**Author:** ![AKhan786](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@AKhan786](https://community.hubspot.com/u/AKhan786)\
**Post date:** [August 11, 2023, 10:29am UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/4 "2023-08-11T10:29:33Z")

</div>

I’ve posted on the slack dev api chat and still can’t figure it out 😞

Was told there to put quotes around my “options” values, as it doesn’t take a JSON.

So I’ve done so (and escaped the other “” via \) so PA accepts it as a valid JSON.

```lia-code-sample
{
 "uri": "https://api.hubapi.com/files/v3/files",
 "method": "POST",
 "headers": {
 "Content-Type": "multipart/form-data"
 },
 "retryPolicy": {
 "type": "Fixed",
 "interval": "PT5S",
 "count": 5
 },
 "authentication": {
 "value": "*sanitized*",
 "type": "Raw"
 },
 "body": {
 "file": "I think it's a base64 string of the file content here"
 "fileName": "SoWTest.xlsx",
 "folderPath": "/",
 "charsetHunch": "UTF-8",
 "options": "{\"access\": \"PRIVATE\", \"duplicateValidationStrategy\": \"NONE\", \"overwrite\": true, \"duplicateValidationScope\": \"EXACT_FOLDER\"}"
 }
}

```

But now I get HTTP 400 Error

```lia-code-sample
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Reason:
<pre> Bad Request</pre></p>
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [August 11, 2023, 11:34am UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/5 "2023-08-11T11:34:17Z")

</div>

Hey @AKhan786,

I found [this post](https://powerusers.microsoft.com/t5/Building-Flows/HTTP-POST-files-with-multipart-form-data/m-p/1238605#M142109) on the PowerAutomate Community discussing how to POST files with multipart/form-data. It’s not specific to HubSpot but it might help in terms of how the HTTP request was configured.

From what I can see your request looks fine and should work. I think the issue might be how the HTTP module in PowerAutomate is processing the file and hence the 400 error being returned.

---

<div class="post-metadata">

**Author:** ![AKhan786](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@AKhan786](https://community.hubspot.com/u/AKhan786)\
**Post date:** [August 11, 2023, 2:29pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/6 "2023-08-11T14:29:12Z")

</div>

Hallelujah! You pointed me on the right path, and after a lot more googling along that line, trial & error testing, I stumbled accross the answer 😄

The key things are:

1. Format the request body as a multi-part form.
  - It’s confusing and complex, and I don’t fully understand (like why certain things like the fileName works in the header, but folderPath needs to be in the body) but hey - it works!

2. The file content needs to be converted from Base64 (or whatever format you might have gotten it in) into Binary.

This is what the request looks like:

 ![AKhan786_0-1691764011373.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/5/5f4eaaacc11731c682394f7118bccdb01c9c15e7.png)

And this is the code for copy/paste:

(Bear in mind the file body is dynamic content so you’d replace that with your binary file data)

```lia-code-sample
{
"$content-type": "multipart/form-data",
"$multipart": [
{
"headers": {
 "Content-Disposition": "form-data; name=\"options\""
 },
 "body": "{\"access\": \"PRIVATE\"}"
},
 {
 "headers": {
 "Content-Disposition": "form-data; name=\"file\"; fileName=\"Test.pdf\""
 },
 "body": @{base64ToBinary(outputs('Get_file_content')?['body']?['$content'])}
 },
 {
 "headers": {
 "Content-Disposition": "form-data; name=\"folderPath\""
 },
 "body": "/Uploaded Documents"
 }
 ]
}

```

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [August 11, 2023, 2:39pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/7 "2023-08-11T14:39:01Z")

</div>

Glad you got it working @AKhan786 😊

---

<div class="post-metadata">

**Author:** ![FAdmin5](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@FAdmin5](https://community.hubspot.com/u/FAdmin5)\
**Post date:** [November 23, 2023, 11:13pm UTC](https://community.hubspot.com/t/uploading-a-file-using-power-automate/87129/8 "2023-11-23T23:13:25Z")

</div>

Thank you this is amazing just solved 8 hours of hair pulling not sure why it works but my god it does well done!!
