1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[WP REST API v2] Pythonで画像をアップロード

Posted at

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']
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?