Last active
February 25, 2019 02:58
-
-
Save NanoDano/d8a82d1634d29454d6bb869274420b23 to your computer and use it in GitHub Desktop.
Drupal 8 REST API example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires all 4 of the REST modules to be turned on | |
# HAL | |
# HTTP Basic Authentication | |
# RESTful Web Services | |
# Serialization | |
import requests | |
import json | |
auth = ('nanodano', 'password') | |
host = 'http://127.0.0.1:8000' | |
url = host + '/entity/node/' | |
headers = { | |
'Content-Type': 'application/hal+json', | |
'Cache-Control': 'no-cache', | |
'Accept': 'application/hal+json', | |
} | |
query = { | |
'_format': 'hal_json', | |
} | |
data = { | |
'_links': {'type': {'href': host + '/rest/type/node/article'}}, | |
'type': [{'target_id': 'article'}], | |
'body': [{'value': 'Test body.'}], | |
'title':[{'value': 'Test title'}], | |
} | |
response = requests.post(url, | |
params=query, | |
data=json.dumps(data).encode('utf-8'), | |
auth=auth, | |
headers=headers) | |
print(response.status_code) | |
print(response.text) | |
# This is the equivalent CURL request that will do the same thing | |
curl = ''' | |
curl --request POST \ | |
-k \ | |
-i \ | |
-s \ | |
--user "nanodano:password" \ | |
--header 'Content-type: application/hal+json' \ | |
-H 'Cache-Control: no-cache' \ | |
'%s/entity/node/?_format=hal_json' \ | |
--data-binary '%s' ''' % (host, json.dumps(data)) | |
print(curl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment