Importing pandas dataframe data to HubSpot CRM

36 Views Asked by At

I need to send data which is in a pandas dataframe df. I need to import this data to Hubspot

from io import StringIO
csv_data = StringIO()
df.to_csv(csv_data, index=False)
csv_data.seek(0)

import requests
import json
# Your HubSpot API Key
api_key = 'api_key'

# HubSpot Import API endpoint
endpoint = 'https://api.hubapi.com/crm/v3/imports'

# Create a payload
payload = {
    'name': 'data source',
    'files': [
        {
              'file_name': 'datasource.csv',
              'fileData': csv_data.read()
        }

    ]
}

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}

# Make the request
response = requests.post(endpoint, json=payload, headers=headers)

if response.status_code == 200:
    print('Data import successful!')
else:
    print(f'Data import failed with status code {response.status_code}: {response.text}')

I am getting this error:

Data import failed with status code 415: <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Reason:
<pre>    Unsupported Media Type</pre></p>
</body>
</html>

The endgoal is to get the data from the dataframe in HubSpot. Any way to resolve this?

0

There are 0 best solutions below