Using python to upload file

Hi, I have been using the legacy API to upload a static file to a folder in hubspot. As the legacy API & the hapi key is expired, I am now trying to use the new API.

 import requests
 import json
 import os

 current_dir = os.getcwd()
 filename = current_dir + '\\loc.csv'

 file_options = {
 'access': 'PUBLIC_INDEXABLE',
 'ttl': 'P3M',
 "overwrite": True,
 'duplicateValidationStrategy': 'NONE',
 'duplicateValidationScope': 'EXACT_FOLDER'
 }

 f = open(filename, 'rb')

 files_data = {
 'file': (filename, f, 'application/octet-stream'),
 'options': (None, json.dumps(file_options), 'text/strings'),
 'folderPath': (None, '/google_store_locator', 'text/strings')
 }

 url = "https://api.hubapi.com/files/v3/files?after=124152"
 YOUR_ACCESS_TOKEN = "***"
 headers = {'content-type': 'application/json',
 'authorization': 'Bearer %s' % YOUR_ACCESS_TOKEN}
 r = requests.post(url, headers=headers, data=files_data)

However, the requests outputted the below error message:

Out[12]: ‘<html>\n<head>\n<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”/>\n<title>Error 415 Unsupported Media Type</title>\n</head>\n<body><h2>HTTP ERROR 415</h2>\n<p>Reason:\n<pre> Unsupported Media Type</pre></p>\n</body>\n</html>\n’

What would be the right way to pass a file to the post job?

Hi, @SPark6 :waving_hand: Thanks for your question. To confirm, is the endpoint you’ve been using? https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file
If so, the endpoint isn’t depreciated, only use of the HAPI key.

Have you tried using a Private App for Authorization in your header for the endpoint you previously used?

Hey, @ChrisoKlepke @tominal do either one of y’all have an example request you can share with @SPark6?

Best,

Jaycee