I think I’ve got this 90% working, but it ends up ‘uploading’ a blank transparent image. I get a 201 response after the upload. I think that’s probably a proxy for when WP finds a missing image. I’m unsure if i’m passing the image incorrectly (ie it doesn’t leave my computer) or if I’m not tagging it properly to WP’s liking.

from base64 import b64encode
import json
import requests

def imgUploadREST(imgPath):
    url = 'https://www.XXXXXXXXXX.com/wp-json/wp/v2/media'
    auth = b64encode('{}:{}'.format('USERNAME','PASS'))
    payload = {
        'type': 'image/jpeg',  # mimetype
        'title': 'title',
    headers = {
        'Content-Disposition' : 'attachment; filename=image_20170510.jpg',
        'Authorization': 'Basic {}'.format(auth),
    with open(imgPath, "rb") as image_file:
        files = {'field_name': image_file}
        r = requests.post(url, files=files, headers=headers, data=payload) 
        print r
        response = json.loads(r.content)
        print response
    return response

I’ve seen a fair number of answers in php or node.js, but I’m having trouble understanding the syntax in python. Thank you for any help!

Read more here: How to upload images using wordpress REST api in python?


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: