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']