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

  • Python 3.8
  • Drupal 9.26
  • HAL
  • HTTP Basic Auth
  • JSON:API
  • RESTful Web Services
  • Serialization
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())

--

--

--

I occasionally write about software, web, blockchain, machine learning, random thoughts.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Starting A Career As A Web Developer

How To Setup Exercism CLI To Use Your Own Github Repository

Difference between Single Value Lookup , Multi Value Lookup & Joins.

How the best Airtable feature is also its worst and how to overcome it.

Production deploy of a Single Page App using S3 and CloudFront

The architecture of sibsforever.org

[Error] Journey of Installation Jazzy

The Java Condundrum

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
Weiming Chen

Weiming Chen

I occasionally write about software, web, blockchain, machine learning, random thoughts.

More from Medium

APIs 101 using Postman

Docker — A Beginner’s Guide to Understanding and Building Docker Images

How to Deploy a Factory Pattern Database Flask Application in Vercel

Factory Photo

Application Frameworks