Powershell text file upload

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
and https://developers.hubspot.com/docs/api/files/files.

Any help is greatly appreciated.

This is my code:

#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

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

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. Note it’s in Python, but hopefully it’ll give you a template to translate from and do some further testing with:

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, 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

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.

#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