Skip to content

Instantly share code, notes, and snippets.

@adorsk
Created November 6, 2012 18:40
Show Gist options
  • Save adorsk/4026625 to your computer and use it in GitHub Desktop.
Save adorsk/4026625 to your computer and use it in GitHub Desktop.
Python posting to Drupal, via REST services
import httplib2
import json
import time
base_url = 'http://localhost/services_test/api/external_tasks'
def t2():
login_data = {
'username': 'tasks',
'password': 'tasks'
}
headers = {'Content-type': 'application/json'}
http = httplib2.Http()
response, json_content = http.request(
base_url + '/user/login.json',
'POST',
headers=headers,
body=json.dumps(login_data)
)
session_cookie = response['set-cookie']
headers['Cookie'] = session_cookie
node_url = base_url + '/node'
r, c = http.request(
node_url + '/1.json',
'GET',
headers=headers
)
node_data = {
'title': 'Task.%s' % time.time(),
'type': 'external_task',
'field_data': {
'und': [
{"value": "Data",}
],
},
}
r, c = http.request(
node_url,
'POST',
headers=headers,
body=json.dumps(node_data)
)
node_c = json.loads(c)
nid = node_c['nid']
update_data = {
'field_data': {
'und': [
{"value": "New Data",}
],
},
}
r, c = http.request(
node_url + '/' + nid,
'PUT',
headers=headers,
body=json.dumps(update_data)
)
print r, c
if __name__ == '__main__':
t2()
@drupalista-br
Copy link

The authentication does not work apparently because of the cookie's domain. Do you know how to get around it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment