[WP REST API v2] Upload images in Python

Note that I was addicted to uploading images to media with the WP REST API.

import requests

user_id = ''
app_password = ''
end_point_url = 'http://example.com/wp-json/wp/v2/media'

image_path = '/path/to/image.jpg'
file_name = 'image.jpg'

headers = {
    'Content-Disposition': f'attachment; filename="{file_name}"'
}

# load image file
f = open(image_path, 'rb')
img_data = f.read()
f.close()

response = requests.post(end_point_url, data=img_data, headers=headers, auth=(user_id, app_password))

# media_id = response.json()['id']

Recommended Posts

[WP REST API v2] Upload images in Python
Quickly implement REST API in Python
Run Google Analytics API (core v3) in python
Anonymous upload of images using Imgur API (using Python)
C API in Python 3
Python beginners tried implementing REST API in one day
Upload JPG file using Google Drive API in Python
Get LEAD data using Marketo's REST API in Python
Hit Mastodon's API in Python
Blender Python API in Houdini (Python 3)
[LINE Messaging API] Issue channel access token v2.1 in Python
Specification generation and code generation in REST API development (Python edition)
Number recognition in images with Python
Pixel manipulation of images in Python
Getting the arXiv API in Python
How to collect images in Python
Hit the Sesami API in Python
Create Gmail in Python without API
Extract text from images in Python
Hit the web API in Python
Access the Twitter API in Python
Working with DICOM images in Python
Cloud DevOps Cookbook Part 4-Explore DevOps DirectMail in Python with REST API
Image upload & download to Azure Storage. With Python + requests + REST API
Regularly upload files to Google Drive using the Google Drive API in Python
Mouse operation using Windows API in Python
Try using the Kraken API in Python
Mosaic images in various shapes (Python, OpenCV)
Tweet using the Twitter API in Python
Get Google Fit API data in Python
[Python] REST API essential, convenient library summary
Development and deployment of REST API in Python using Falcon Web Framework
Download images from URL list in Python
Quickly try Microsoft's Face API in Python
Detect Japanese characters from images using Google's Cloud Vision API in Python
Try hitting the YouTube API in Python
Implemented Python wrapper for Qiita API v2
Get rid of DICOM images in Python
Read text in images with python OCR
Upload images to Google Drive with Python
Touch the sample v20-python-samples of the OANDA v20 REST API wrapper library for Python
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
Receive websocket of kabu station ® API in Python
Convert PDFs to images in bulk with Python
Try using the BitFlyer Ligntning API in Python
Get image URL using Flickr API in Python
Tips for hitting the ATND API in Python
Let's judge emotions using Emotion API in Python
Getting rid of DICOM images in Python Part 2
Load images from URLs using Pillow in Python 3
How to create a Rest Api in Django
Redis Shortest Introduction (3) API in 5 minutes even in Python
Hit the Firebase Dynamic Links API in Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python