Using Python to Post json contact list to Qualtrics API, error with Content-Type
<p>I'm trying to import contacts into a contact list in Qualtrics. I am using python to do this. </p>

<pre><code>Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'

data = open('contacts.json', 'rb')
headers = {'X-API-TOKEN': Token, 'Content-Type':'application/json',}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports', headers=headers, data=data)
r.text
</code></pre>

<p>This code gives me the following error: '{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Invalid Content-Type. expected=multipart/form-data found=application/json","errorCode":"RP_0.1"},"requestId":null}}'</p>

<p>I changed the content type to multipart/form-data that it says it is expecting and received the response "413", which qualtrics explains means "The request body was too large. This can also happen in cases where a multipart/form-data request is malformed."</p>

<p>I have tested my json and verified that it is valid. Also, I don't know why the request body would be too large because it's only 13 contacts that I'm trying to import. Any ideas?</p>

<p>With the help of Qualtrics Support, I was eventually able to get the following code to work:</p>

<pre><code>Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'

url = "https://az1.qualtrics.com/API/v3/mailinglists/" + ContactsID + "/contactimports/"
headers = {
   'content-type': "multipart/form-data; boundary=---BOUNDRY",
   'x-api-token': "Token"
   } 
files = {'contacts': ('contacts', open('contacts.json', 'rb'), 'application/json')}
request = requests.post(url, headers=headers, files=files)
print(request.text)
</code></pre>

<p>Please note that if you want to use this code, you will need to change "az1" in the URL to your own Qualtrics datacenter ID.</p>

python json content-type qualtrics