API Contact Import Error (Error 400 Bad Request)

Hi everyone, I’m trying to use API to do the following thing: import contacts from CSV file in my local disk. But, I get Error 400 Bad Request and I don’t know why. I checked everything but it seems like everything is correct.

Here is my code:

date_subdir= "2022-10-17" #subdirectory in which my file is located
filename= "2022-09-17 to 2022-10-16 match.csv"
parent_dir= r"H:\My Drive\1. Digital Marketing\sync hubspot erwin master"

from pathlib import Path
import requests
import json
import os

url = "https://api.hubapi.com/crm/v3/imports"

ACCESS_TOKEN = "my private app access token";

headers = {
 'content-type': 'multipart/form-data',
 'authorization': 'Bearer %s' % ACCESS_TOKEN
}

data = {
 "name": filename.split('.')[0], #returns 2022-09-17 to 2022-10-16 match, the import name i want
 "files": [
 {
 "fileName": filename, #returns 2022-09-17 to 2022-10-16 match.csv
 "fileFormat": "CSV",
 # "dateFormat": "DAY_MONTH_YEAR", #there's no date in my file though
 "fileImportPage": {
 "hasHeader": True,
 "columnMappings": [
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Record ID",
 "propertyName": "hs_object_id",
 "idColumnType": "HUBSPOT_OBJECT_ID"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Phone Number",
 "propertyName": "phone",
 "idColumnType": False
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Email",
 "propertyName": "email",
 "idColumnType": "HUBSPOT_ALTERNATE_ID"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Lifecycle Stage",
 "propertyName": "lifecyclestage",
 "idColumnType": False
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Learning Preference", #a custom property i made for my use
 "propertyName": "learning_preference",
 "idColumnType": False
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Is TMK Call?",#a custom property i made for my use
 "propertyName": "is_tmk_call_",
 "idColumnType": False
 }
 ]
 }
 }
 ]
}

datastring = json.dumps(data)

payload = {"importRequest": datastring}

absolute_file_path = Path(parent_dir, date_subdir, filename) #returns WindowsPath('H:/My Drive/1. Digital Marketing/sync hubspot erwin master/2022-10-17/2022-09-17 to 2022-10-16 match.csv'), which is correct

files = [
 ('files', open(absolute_file_path, 'rb'))
]
print(files)

response = requests.request("POST", url, data=payload, files=files, headers=headers)

print(response.text.encode('utf8'))
print(response.status_code)

the error i get:
[('files', <_io.BufferedReader name='H:\\My Drive\\1. Digital Marketing\\sync hubspot erwin master\\2022-10-17\\2022-09-17 to 2022-10-16 match.csv'>)]
Error 400 Bad Request

Any idea? Thanks in advance.

Hi, @danjelito :waving_hand: Have you tried using Postman as shown in the example here? — Start an import

It may help us root out any additional issues.

Best,

Jaycee

Hi @Jaycee_Lewis , not yet, currently I’m trying to use Python from my local computer to run above script. Do I have to use Postman to run this?

This is an old post. Did you ever get it worked out? Usually a 400 error includes Content that contains an error message. Are you able to see that with this error?

Hi Anya, I’m encountering similar issues, and I’m wondering if you’ve figured this out? Thanks!