Upload contact list

Hello

I'm trying to upload new records to a contact list using python, but I am not sure how I have to make the request (I always get 400 error, bad request) . Here is the code I've tried:

url = "https://apps.mypurecloud.ie/uploads/v2/contactlist"
with open("example.csv", "rb") as f:
    headers = {'Authorization': 'Bearer ' + token}
    body = {
        "id" : contact_list_id,
        "file": f.read(),
        "fileType" : "contactlist",
        "contact-id-name": "Id"

    }
    r = requests.post(url, data=body, headers=headers)

Could someone tell me how can I do the request?
Thank you

Hello,

The request's content-type must be multipart/form-data.

I am a newbie on Python so I hope I haven't forgotten something below.
I just tried the following and it seems to work.

2 things to change in your request: files=body in the request (files is to have multipart/form-data), and use of tuples (None, xxxx) for parameters other than file.

body = {
    "id": (None, contact_list_id),
    "file": f.read(),
    "fileType": (None, "contactlist"),
    "contact-id-name": (None, "Id")
}
r = requests.post(url, files=body, headers=headers)
1 Like

import requests
import traceback
try :
auth_token = 'UR AUTH TOKEN'
url = 'https://apps.mypurecloud.com/uploads/v2/contactlist'
data = {'id':'ID of the file that you created with headers', 'fileType':'contactlist', 'contact-id-name':'UR CONTACT ID Field '}
files = {'file': open('test.csv', 'rb')}
#files = [ ('file', (filename, open('test.csv','rb'), text/csv)) ]
headers = {'Authorization': 'Bearer '+auth_token}
r = requests.post(url, data=data, files=files, headers=headers, verify=True)
print(r.status_code)
print(r.headers)
print(r.content);
except Exception as er:
print('exception')
traceback.print_exc()

1 Like

Hello

Both solutions worked!! Thank you for your help :grin:

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.