0

I am trying to update an image using the Wordpress rest api in Python. I get the status code 200 but the image does not update. I have not found any solution online other than replacing http with https.

def restImgUL(imgPath, imgId=''): url='https://example.com/wp-json/wp/v2/media/' + imgId data = open(imgPath, 'rb').read() fileName = os.path.basename(imgPath) user = "" password = "" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) res = requests.post(url=url, data=data, headers={'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'image/jpg','Content-Disposition' : 'attachment; filename=%s'% fileName, }) print(res.status_code) print(res.text) 
1
  • are you sure it's possible to do this in WordPress without custom PHP code? This isn't possible using the WordPress admin GUI and it's something that usually requires a 3rd party plugin to explicitly enable. The best you can do in WP Admin is editing an image
    – Tom J Nowell
    CommentedMay 10, 2023 at 12:26

2 Answers 2

0

What you are trying to do is not something that endpoint is capable of. You can create attachments, you can even rotate and crop the file, but you cannot replace the attached file.

When cropping/scaling/rotating an image, a new attachment and ID are created for the newly edited image.

However, sending an UPDATE/PUT/POST request to an attachments endpoint with a new file will not replace it with that file.

Here is the relevant code: in WordPress in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php, the edit_media_item method:

https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L428-L676

How Do I Do It Then?

You don't, and you shouldn't.

Core provides no method for performing this action. There are plugins that allow you to replace an uploaded file but these are aimed at WP Admin, not the REST API, you will need to test and evaluate each on a per plugin basis, or build your own endpoint.

Note that replacing the contents of a file without changing its URL means browser cache and CDN caches won't be updated cleanly, some CDNs may not support changing the contents at all ( e.g. Jetpacks Photon ), and Google/Search engines will be unaware.

If you need a single URL that always points to the latest version of a document, you should use PHP to handle that request and redirect to the actual file, or to serve that file. Replacing the file in the filesystem then relying on Nginx/Apache/etc to serve that file will give you problems.

    0

    I had a similar problem and could solve it using the PUT method instead of POST, so you might want to try this approach aswell to update an image using the Wordpress rest api in Python. ;-)

    def restImgUL(imgPath, imgId=''): url = 'https://example.com/wp-json/wp/v2/media/' + imgId data = open(imgPath, 'rb').read() fileName = os.path.basename(imgPath) user = "" password = "" credentials = user + ':' + password token = base64.b64encode(credentials.encode()) res = requests.put(url=url, data=data, headers={'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'image/jpg', 'Content-Disposition': 'attachment; filename=%s' % fileName, }) print(res.status_code) print(res.text) 

    Additionally...

    • Check if the imgId parameter is correct: If the imgId parameter is incorrect, the API call will still return a 200 status code, but the image will not be updated. Make sure that the imgId parameter corresponds to the ID of the image you want to update.
    • Check if the file format is supported: WordPress supports a wide
      range of file formats, but it's possible that the file format you are trying to upload is not supported. Check if the file format of the
      image you are trying to upload is supported by WordPress.
    • Check if the file size is too large: There might be a limit on the
      file size that you can upload to WordPress. Check if the size of the image you are trying to upload is within the limit.
    1
    • The image is still not updating. Maybe some option that you might have changed?
      – Shary
      CommentedMay 10, 2023 at 12:15

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.