Use Python to call Drupal 9 core RESTful API to create new content

import requests
from requests.auth import HTTPBasicAuth
endpoint = 'https://d9.site/jsonapi/node/article'

u = 'username'
p = 'password'
headers = {
'Accept': 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json'
}
payload = {
"data": {
"type": "node--article",
"attributes": {
"title": "What's up from Python",
"body": {
"value": "Be water. My friends.",
"format": "plain_text"
}
}
}
}
r = requests.post(endpoint, headers=headers, auth=(u, p), json=payload)
print(r.headers)
print(r.status_code)
print(r.json())

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store