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

OWASP- Juice Shop

Best API to monitor the CSR of your company

Integrating Etsy sale alerts to Discord

AutoMapper in ASP.NET Core

Alphabet To Number , Number To Alphabet ON PHP

CS371p Spring 2022 Week 9: Grace Zhu

READ/DOWNLOAD%+ Linux Kernel Development FULL BOOK PDF & FULL AUDIOBOOK

DBpedia + SQL = timbr-DBpedia Open Knowledge Graph

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

Soap Vs Rest, Which API is More Secure? Updated 2021.

Basic git commands: Learn how to use git.

Explaining SAML With Example

Getting Started: GitHub Workflow + Collaborating