Skip to content

Instantly share code, notes, and snippets.

@tertek
Created February 8, 2021 11:07
Show Gist options
  • Save tertek/80607e3843abd8da3cfc97c3543b1e1f to your computer and use it in GitHub Desktop.
Save tertek/80607e3843abd8da3cfc97c3543b1e1f to your computer and use it in GitHub Desktop.
Example of using REDCap API for exporting record data and importing into Stata via Python
python:
#!/usr/bin/env python
import requests
data = {
'token': '<PLEASE INSERT API TOKEN HERE>',
'content': 'record',
'format': 'csv',
'type': 'flat',
'csvDelimiter': '',
'records[0]': '1',
'forms[0]': 'contact_information',
'rawOrLabel': 'raw',
'rawOrLabelHeaders': 'raw',
'exportCheckboxLabel': 'false',
'exportSurveyFields': 'false',
'exportDataAccessGroups': 'false',
'returnFormat': 'json'
}
# Send Post Request to Redcap API
r = requests.post('https://redcap-dev.swisstph.ch/redcap/api/',data=data)
# Write request contents into temporary file
f = open("import.csv", "w")
f.write(r.text)
f.close()
end
import delimited import.csv, clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment