<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python - help uploading a file via CMS API in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712547#M58313</link>
    <description>&lt;P&gt;Thank you very much it worked! I've uploaded it and associated with deal using v1 engagement considering the file as a note.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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):&lt;/P&gt;&lt;PRE&gt;import io&lt;BR /&gt;csv_buffer = io.BytesIO()&lt;BR /&gt;df.to_csv(csv_buffer&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;sep&lt;/SPAN&gt;=&lt;SPAN&gt;","&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;index&lt;/SPAN&gt;=&lt;SPAN&gt;False, &lt;/SPAN&gt;&lt;SPAN&gt;mode&lt;/SPAN&gt;=&lt;SPAN&gt;"wb"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;encoding&lt;/SPAN&gt;=&lt;SPAN&gt;"ISO-8859-1"&lt;/SPAN&gt;)&lt;BR /&gt;csv_buffer.seek(&lt;SPAN&gt;0&lt;/SPAN&gt;)&lt;BR /&gt;&lt;BR /&gt;files = {&lt;SPAN&gt;'file'&lt;/SPAN&gt;: csv_buffer}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 31 Oct 2022 23:26:09 GMT</pubDate>
    <dc:creator>PedroTayer</dc:creator>
    <dc:date>2022-10-31T23:26:09Z</dc:date>
    <item>
      <title>Python - help uploading a file via CMS API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/711923#M58247</link>
      <description>&lt;P&gt;Hi guys, I'm trying to upload a .csv file to file manager via &lt;A href="https://developers.hubspot.com/docs/api/files/files" target="_blank" rel="noopener"&gt;https://developers.hubspot.com/docs/api/files/files&lt;/A&gt; but I get a 400. The lack of example on documentation made me come here.&lt;/P&gt;&lt;P&gt;My headers and url are:&lt;/P&gt;&lt;PRE&gt;headers = {&lt;SPAN&gt;'content-type' :&lt;/SPAN&gt; &lt;SPAN&gt;'multipart/form-data', 'authorization': myauth}&lt;BR /&gt;url = 'https://api.hubapi.com/files/v3/files'&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Im trying to:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;with &lt;/SPAN&gt;&lt;SPAN&gt;open&lt;/SPAN&gt;(path_to_csv_file) &lt;SPAN&gt;as &lt;/SPAN&gt;fh:&lt;BR /&gt;    data = {&lt;SPAN&gt;'file'&lt;/SPAN&gt;: fh&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;            &lt;SPAN&gt;'folderId'&lt;/SPAN&gt;: &lt;SPAN&gt;'89858067367'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;            &lt;SPAN&gt;'fileName'&lt;/SPAN&gt;: &lt;SPAN&gt;'test'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;            &lt;SPAN&gt;'options'&lt;/SPAN&gt;: json.dumps({&lt;BR /&gt;                &lt;SPAN&gt;'access'&lt;/SPAN&gt;: &lt;SPAN&gt;'PRIVATE'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;                &lt;SPAN&gt;'overwrite'&lt;/SPAN&gt;: &lt;SPAN&gt;False,&lt;BR /&gt;&lt;/SPAN&gt;                &lt;SPAN&gt;'duplicateValidationStrategy'&lt;/SPAN&gt;: &lt;SPAN&gt;'NONE'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;                &lt;SPAN&gt;'duplicateValidationScope'&lt;/SPAN&gt;: &lt;SPAN&gt;'EXACT_FOLDER'&lt;BR /&gt;&lt;/SPAN&gt;            })&lt;BR /&gt;            }&lt;BR /&gt;&lt;BR /&gt;    req = requests.post(url&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;files&lt;/SPAN&gt;=data&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;headers&lt;/SPAN&gt;=headers)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Where am I goind wrong?&lt;/P&gt;</description>
      <pubDate>Sat, 29 Oct 2022 14:55:20 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/711923#M58247</guid>
      <dc:creator>PedroTayer</dc:creator>
      <dc:date>2022-10-29T14:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: Python - help uploading a file via CMS API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712374#M58296</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/247517"&gt;@PedroTayer&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Thanks for reaching out! Hey,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/144301"&gt;@taran42&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/193060"&gt;@JBeatty&lt;/a&gt;, do&amp;nbsp;you have any experience or troubleshooting tips you can share with&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/247517"&gt;@PedroTayer&lt;/a&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much! — Jaycee&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2022 15:42:14 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712374#M58296</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2022-10-31T15:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Python - help uploading a file via CMS API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712487#M58307</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/247517"&gt;@PedroTayer&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;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)
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2022 19:33:29 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712487#M58307</guid>
      <dc:creator>JBeatty</dc:creator>
      <dc:date>2022-10-31T19:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: Python - help uploading a file via CMS API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712547#M58313</link>
      <description>&lt;P&gt;Thank you very much it worked! I've uploaded it and associated with deal using v1 engagement considering the file as a note.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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):&lt;/P&gt;&lt;PRE&gt;import io&lt;BR /&gt;csv_buffer = io.BytesIO()&lt;BR /&gt;df.to_csv(csv_buffer&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;sep&lt;/SPAN&gt;=&lt;SPAN&gt;","&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;index&lt;/SPAN&gt;=&lt;SPAN&gt;False, &lt;/SPAN&gt;&lt;SPAN&gt;mode&lt;/SPAN&gt;=&lt;SPAN&gt;"wb"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;encoding&lt;/SPAN&gt;=&lt;SPAN&gt;"ISO-8859-1"&lt;/SPAN&gt;)&lt;BR /&gt;csv_buffer.seek(&lt;SPAN&gt;0&lt;/SPAN&gt;)&lt;BR /&gt;&lt;BR /&gt;files = {&lt;SPAN&gt;'file'&lt;/SPAN&gt;: csv_buffer}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2022 23:26:09 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Python-help-uploading-a-file-via-CMS-API/m-p/712547#M58313</guid>
      <dc:creator>PedroTayer</dc:creator>
      <dc:date>2022-10-31T23:26:09Z</dc:date>
    </item>
  </channel>
</rss>

