Search code examples
pythonhttpcurlqualtrics

Qualtrics Import Survey - Python implementation of cURL


I'm trying to write a function to import a survey into qualtrics using their V3 api. They've given the following in their documentation

curl -H 'X-API-TOKEN: yourapitoken' \
-F 'name=Test' \
-F 'file=@/path/to/MySurvey.qsf;type=application/vnd.qualtrics.survey.qsf' \
  'https://yourdatacenterid.qualtrics.com/API/v3/surveys'

I'm trying to implement this in python using the requests library.

I'm not quite sure how to pass the 'file=@/path/to/MySurvey.qsf;type=application/vnd.qualtrics.survey.qsf' to requests.get()

Current setup:

headers = {'X-API-TOKEN':'xxxxxxxx'}
base_url = 'http://coN.qualtrics.com/API/v3/surveys/'
files = [
       ('name','New Survey'),
       ('file', ......),
       ]
test = requests.get(base_url, headers, files)

What do I replace ..... with to get this working?

Qualtrics API Documentation : https://api.qualtrics.com/docs/import-survey-1


Solution

  • Firstly, i think you should send a post request .
    Then you need data for the post data, and files for the file .

    base_url = 'http://coN.qualtrics.com/API/v3/surveys/'
    headers = {'X-API-TOKEN':'xxxxxxxx'}
    data = {'name':'New Survey'}
    files = {'file':('File Name', open('File Path', 'rb'), 'application/vnd.qualtrics.survey.qsf') }
    test = requests.post(base_url, headers=headers, data=data, files=files)
    
    print(test.status_code)
    print(test.json())
    

    In the files dict, 'File Name' is the name of the file, followed by the file object, and then the 'content_type' . You will find more info in the docs