Skip to content

Instantly share code, notes, and snippets.

@speg
Created July 29, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speg/f03a0f02dae94b55de23 to your computer and use it in GitHub Desktop.
Save speg/f03a0f02dae94b55de23 to your computer and use it in GitHub Desktop.
Create and update a response.
import requests # using http://docs.python-requests.org to make HTTP requests
API_KEY = XXXXXXXXXXXX # your credentials
PASSWORD = XXXXXXXXXXX
ENDPOINT = 'https://app.fluidsurveys.com/api/v2/surveys/209179/responses/'
auth = requests.auth.HTTPBasicAuth(API_KEY, PASSWORD)
headers = {'Content-Length': '0'} # explictly state Content-Length for empty requests POST
r.get(ENDPOINT, auth=auth) # get current responses
r.json
#{u'count': 1,
# u'id': u'209179',
# u'responses': [{u'EDuzkU9wxC': u'',
# u'Q4slQNgep3': [],
# u'X1qx3SM986': u'',
# u'_completed': 0,
# u'_created_at': u'2013-07-27T20:07:22-04:00',
# u'_get_variables': u'IMPORTED&',
# u'_id': 23753188,
# u'_ip_address': u'0.0.0.0',
# u'_key': u'815608c6f7a3daa2aff58b11323d7abb8926f5cd',
# u'_language': u'',
# u'_pagepath': u'',
# u'_updated_at': u'2013-07-27T20:07:22-04:00',
# u'_weighted_score': u'',
# u'aSlpuwAveF': [None, None],
# u'g5V6sF5EDf': [None, None],
# u'veBf9qAUqP': [None, None]}],
# u'start': 0,
# u'total': 1}
r = requests.post(ENDPOINT, headers=headers, auth=auth) #create a new response
r.json
#{u'id': 23865955,
# u'key': u'd4f21ee8151d662348f4e2901bae806d071842eb',
# u'success': True,
# u'url': u'http://app.fluidsurveys.com/surveys/marius-constantin/testsurvey/d4f21ee8151d662348f4e2901bae806d071842eb/'}
data = {'_key': 'd4f21ee8151d662348f4e2901bae806d071842eb', 'X1qx3SM986': 'Hello World'}
r = requests.post(ENDPOINT, auth=auth, data=data) # update the response we just made
r.json
#{u'id': 23865955,
# u'key': u'd4f21ee8151d662348f4e2901bae806d071842eb',
# u'success': True,
# u'url': u'http://app.fluidsurveys.com/surveys/marius-constantin/testsurvey/d4f21ee8151d662348f4e2901bae806d071842eb/'}
r.get(ENDPOINT, auth=auth) # check current responses to see if our update worked
r.json
#{u'count': 2,
# ...
# u'X1qx3SM986': u'Hello World',
# ...
# u'total': 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment