Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload images using wordpress REST api in python?

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',
        "Content":"content",
        "excerpt":"Excerpt",
    }
    headers = {
        'post_content':'post_content',
        'Content':'content',
        '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!

like image 525
my Year Of Code Avatar asked May 11 '17 12:05

my Year Of Code


2 Answers

I've figured it out!

With this function I'm able to upload images via the WP REST api to my site (Photo Gear Hunter.) The function returns the ID of the image. You can then pass that id to a new post call and make it the featured image, or do whatever you wish with it.

def restImgUL(imgPath):
    url='http://xxxxxxxxxxxx.com/wp-json/wp/v2/media'
    data = open(imgPath, 'rb').read()
    fileName = os.path.basename(imgPath)
    res = requests.post(url='http://xxxxxxxxxxxxx.com/wp-json/wp/v2/media',
                        data=data,
                        headers={ 'Content-Type': 'image/jpg','Content-Disposition' : 'attachment; filename=%s'% fileName},
                        auth=('authname', 'authpass'))
    # pp = pprint.PrettyPrinter(indent=4) ## print it pretty. 
    # pp.pprint(res.json()) #this is nice when you need it
    newDict=res.json()
    newID= newDict.get('id')
    link = newDict.get('guid').get("rendered")
    print newID, link
    return (newID, link)
like image 187
my Year Of Code Avatar answered Nov 10 '22 23:11

my Year Of Code


To specify additional fields supported by the api such as alt text, description ect:

from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import os
fileName = os.path.basename(imgPath)
multipart_data = MultipartEncoder(
    fields={
        # a file upload field
        'file': (fileName, open(imgPath, 'rb'), 'image/jpg'),
        # plain text fields
        'alt_text': 'alt test',
        'caption': 'caption test',
        'description': 'description test'
    }
)

response = requests.post('http://example/wp-json/wp/v2/media', data=multipart_data,
                         headers={'Content-Type': multipart_data.content_type},
                         auth=('user', 'pass'))
like image 34
TheoretiCAL Avatar answered Nov 10 '22 22:11

TheoretiCAL