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>You need to use <code>files = ..</code> for a <a href="http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file" rel="nofollow">multipart</a> request:</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}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports',files={"file":data}, headers=headers)
r.text
</code></pre>

<p>Once you do requests will take care of the rest:</p>

<pre><code>In [36]: url = 'http://httpbin.org/post' 
In [37]: headers = {'X-API-TOKEN': "123456789"}    
In [38]: files = {'file': open('a.csv', 'rb')}    
In [39]: r = requests.post(url, files=files, headers=headers)

In [40]: print r.text
{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "a,b,c\n1,2,3"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "152", 
    "Content-Type": "multipart/form-data; boundary=3830dbe5fa6141f69d3d85dee4ba6e78", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.10.0", 
    "X-Api-Token": "123456789"
  }, 
  "json": null, 
  "origin": "51.171.98.185", 
  "url": "http://httpbin.org/post"
}


In [41]: print(r.request.body)
--3830dbe5fa6141f69d3d85dee4ba6e78
Content-Disposition: form-data; name="file"; filename="a.csv"

a,b,c
1,2,3
--3830dbe5fa6141f69d3d85dee4ba6e78--
</code></pre>

<p>looking at the <a href="https://api.qualtrics.com/docs/create-contacts-import" rel="nofollow">docs</a>, you actually want something closer to:</p>

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

data = open('contacts.json', 'rb')

files = {'file': ('contact', data ,'application/json', {'X-API-TOKEN': Token})}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports',files=files)
</code></pre>

python json content-type qualtrics