# Powershell text file upload

**URL:** <https://community.hubspot.com/t/powershell-text-file-upload/37944>\
**Category:** APIs & Integrations\
**Tags:** free\
**Created:** [May 26, 2021, 2:28pm UTC](https://community.hubspot.com/t/powershell-text-file-upload/37944 "2021-05-26T14:28:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devin1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/devin1/32/77060_2.png) [@Devin1](https://community.hubspot.com/u/Devin1)\
**Post date:** [May 26, 2021, 2:28pm UTC](https://community.hubspot.com/t/powershell-text-file-upload/37944/1 "2021-05-26T14:28:10Z")

</div>

Hi,

I’m trying to upload a text file (UTF-8) using the Powershell Invoke-RestMethod, but I am getting the “(415) Unsupported Media Type” error. This is my first time trying to interact with the Hubspot API.

I was trying to follow the information from [https://legacydocs.hubspot.com/docs/methods/files/v3/upload\_new\_file](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file)  
and [https://developers.hubspot.com/docs/api/files/files.](https://developers.hubspot.com/docs/api/files/files,)

Any help is greatly appreciated.

This is my code:

```lia-code-sample
#POWERSHELL
$apiKey = 'my api key'
$uri = "https://api.hubapi.com/files/v3/files?hapikey=$apiKey"
$file = Get-Content C:\Temp\testfile.txt -Encoding Byte

$headers = @{accept = 'application/json'}

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

$body = @{
 fileName = 'testfile.txt'
 file = $file
 folderPath = '/test'
 #folderId = 
 charsetHunch = 'utf-8'
 options = $fileOptions 
}

$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $body -ContentType 'application/json' -Verbose
$response.results

```

Thanks

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [May 26, 2021, 6:20pm UTC](https://community.hubspot.com/t/powershell-text-file-upload/37944/2 "2021-05-26T18:20:23Z")

</div>

@zaklein , would you be able to help out here?

---

<div class="post-metadata">

**Author:** ![zaklein](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zaklein/32/44431_2.png) [@zaklein](https://community.hubspot.com/u/zaklein)\
**Post date:** [May 27, 2021, 1:54pm UTC](https://community.hubspot.com/t/powershell-text-file-upload/37944/3 "2021-05-27T13:54:18Z")

</div>

Hi @Devin1

I’m guessing this is related to a mismatch in what the endpoint is expecting and what you’re sending it. The 2 links you’ve provided in your question are for 2 separate API endpoints. You’ll need to craft your request body based on the endpoint you’re using. Here’s an example I successfully tested a while back using the [v3 legacy docs endpoint](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file). Note it’s in Python, but hopefully it’ll give you a template to translate from and do some further testing with:

```lia-code-sample
import requests
import json
endpoint = 'https://api.hubapi.com/filemanager/api/v3/files/upload'
headers = {'Authorization': 'Bearer {{bearer_token}}'} # alternatively you could use your hapikey in the request URL query string for authentication
filename = 'test1.jpg'

file_options = {
 'access': 'PRIVATE',
 'ttl': 'P1M',
 "overwrite": False,
 'duplicateValidationStrategy': 'REJECT',
 'duplicateValidationScope': 'EXACT_FOLDER'
}
files_data = {
 'file': (filename, open(filename, 'rb'), 'application/octet-stream'),
 'options': (None, json.dumps(file_options), 'text/strings'),
 'folderPath': (None, '/parcelhub-attachments', 'text/strings')
}
response = requests.post(endpoint, headers=headers, files=files_data)

```

If you’d prefer to use **POST** /files/v3/files from the [new API docs](https://developers.hubspot.com/docs/api/files/files), you’ll need to craft your request body based on the “parameters” outlined under the relevant endpoint. Let me know how you get on.

All the best,

Zach

---

<div class="post-metadata">

**Author:** ![Devin1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/devin1/32/77060_2.png) [@Devin1](https://community.hubspot.com/u/Devin1)\
**Post date:** [June 15, 2021, 12:00am UTC](https://community.hubspot.com/t/powershell-text-file-upload/37944/4 "2021-06-15T00:00:35Z")

</div>

Thanks for the code Zach. It worked for me.

I gave it another try in Powershell and got it working, too. Here’s my code, in case someone else is trying to do the same in Powershell.

```lia-code-sample
#Powershell

$apiKey = 'your key'
$uri = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=$apiKey"

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

$form = @{
 fileName = 'testfile.txt'
 file = Get-Item C:\Temp\testfile.txt
 folderPath = '/test'
 #folderId = 0
 charsetHunch = 'UTF-8'
 options = $fileOptions | ConvertTo-Json
} 

$response = Invoke-WebRequest -Uri $uri -Method Post -Form $form -Verbose 
$response.results

```
