Answer the question
In order to leave comments, you need to log in
How to upload JPG PNG to WordPress gallery via REST API using Python and requests?
My code
import base64
import json
import os
import requests
# Авторизация. Плагин Application Passwords https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide
# лог/пас сгенерированы в меню Users -> учетка админа -> Пароли приложений
USER = 'wp_api_python'
PASSWORD = '3KDD 91Bh y2Gl qt9H clgZ GEAl'
img_path = "Render_Project_Champery_002.png"
url = "http://suite.ru/wp-json/wp/v2/media"
creds = USER + ':' + PASSWORD
token = base64.b64encode(creds.encode())
header = {
'Authorization': 'Basic ' + token.decode('utf-8'),
"Accept": "application/json",
}
media = {
'file': open(img_path, 'rb'),
'caption': 'First TEST API Image upload',
'description': 'First TEST API Image upload'
}
image = requests.post(url=url, headers=header, files=media)
print(image.json())
{'code': 'rest_cannot_create', 'message': 'Извините, вам не разрешено создавать записи от лица этого пользователя.', 'data': {'status': 401}}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question