Adrian_Santamaria | 2019-09-23 15:07:59 UTC | #1
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" : contactlistid, "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
Jerome.Saint-Marc | 2019-09-23 17:08:54 UTC | #2
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, contactlistid), "file": f.read(), "fileType": (None, "contactlist"), "contact-id-name": (None, "Id") } r = requests.post(url, files=body, headers=headers)
SharmaK | 2019-09-23 19:55:54 UTC | #3
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 '+authtoken} r = requests.post(url, data=data, files=files, headers=headers, verify=True) print(r.statuscode) print(r.headers) print(r.content); except Exception as er: print('exception') traceback.print_exc()
Adrian_Santamaria | 2019-09-24 06:48:23 UTC | #4
Hello
Both solutions worked!! Thank you for your help :grin:
system | 2019-10-25 06:48:27 UTC | #5
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 6086