APIs & Integrations

PedroTayer
Teilnehmer/-in

Python - help uploading a file via CMS API

lösung

Hi guys, I'm trying to upload a .csv file to file manager via https://developers.hubspot.com/docs/api/files/files but I get a 400. The lack of example on documentation made me come here.

My headers and url are:

headers = {'content-type' : 'multipart/form-data', 'authorization': myauth}
url = 'https://api.hubapi.com/files/v3/files'

 Im trying to:

with open(path_to_csv_file) as fh:
data = {'file': fh,
'folderId': '89858067367',
'fileName': 'test',
'options': json.dumps({
'access': 'PRIVATE',
'overwrite': False,
'duplicateValidationStrategy': 'NONE',
'duplicateValidationScope': 'EXACT_FOLDER'
})
}

req = requests.post(url, files=data, headers=headers)

 Where am I goind wrong?

0 Upvotes
1 Akzeptierte Lösung
JBeatty
Lösung
Ratgeber/-in | Diamond Partner
Ratgeber/-in | Diamond Partner

Python - help uploading a file via CMS API

lösung

Hi @PedroTayer,

 

2 things, first you don't want to specify the content type, because for multipart/form-data encoding that contains the boundary and you are overwriting that, and secondly you want to use the files parameter for the post function to tell requests to do multipart/form-data. Here is some sample code I got working:

import json, requests, os
BEARER_TOKEN = os.environ['BEARER_TOKEN']

headers = {'authorization': "Bearer " + BEARER_TOKEN}
url = 'https://api.hubapi.com/files/v3/files'

with open("test.txt") as fh:
  files = {'file': fh}
  data = {
    'folderId': '',
    'fileName': 'testFileName',
    'options': json.dumps({
      'access': 'PRIVATE',
      'overwrite': False,
      'duplicateValidationStrategy': 'NONE',
      'duplicateValidationScope': 'EXACT_FOLDER'
    })
  }

  resp = requests.post(url, files=files, data=data, headers=headers)

 

Best,

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

Lösung in ursprünglichem Beitrag anzeigen

0 Upvotes
3 Antworten
JBeatty
Lösung
Ratgeber/-in | Diamond Partner
Ratgeber/-in | Diamond Partner

Python - help uploading a file via CMS API

lösung

Hi @PedroTayer,

 

2 things, first you don't want to specify the content type, because for multipart/form-data encoding that contains the boundary and you are overwriting that, and secondly you want to use the files parameter for the post function to tell requests to do multipart/form-data. Here is some sample code I got working:

import json, requests, os
BEARER_TOKEN = os.environ['BEARER_TOKEN']

headers = {'authorization': "Bearer " + BEARER_TOKEN}
url = 'https://api.hubapi.com/files/v3/files'

with open("test.txt") as fh:
  files = {'file': fh}
  data = {
    'folderId': '',
    'fileName': 'testFileName',
    'options': json.dumps({
      'access': 'PRIVATE',
      'overwrite': False,
      'duplicateValidationStrategy': 'NONE',
      'duplicateValidationScope': 'EXACT_FOLDER'
    })
  }

  resp = requests.post(url, files=files, data=data, headers=headers)

 

Best,

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

0 Upvotes
PedroTayer
Teilnehmer/-in

Python - help uploading a file via CMS API

lösung

Thank you very much it worked! I've uploaded it and associated with deal using v1 engagement considering the file as a note.

 

Just another thing, might help someone, I've managed to upload the file directly from a dataframe by using (the encoding is according to portuguese-br):

import io
csv_buffer = io.BytesIO()
df.to_csv(csv_buffer, sep=",", index=False, mode="wb", encoding="ISO-8859-1")
csv_buffer.seek(0)

files = {'file': csv_buffer}

 

0 Upvotes
Jaycee_Lewis
Community-Manager/-in
Community-Manager/-in

Python - help uploading a file via CMS API

lösung

Hi, @PedroTayer 👋 Thanks for reaching out! Hey, @taran42 @JBeatty, do you have any experience or troubleshooting tips you can share with @PedroTayer?

 

Thank you very much! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes