WordPress REST API fails with Python requests on MacOS, works on Windows
-
I’ve written a simple Python script to upload an image to a WP site:
import requests import base64 BASE_URL = "https://example.com/wp-json/wp/v2" media = { "file": open("image.png", "rb"), "caption": "a media file", "description": "some media file" } creds = "wp_admin_user" + ":" + "app password" token = base64.b64encode(creds.encode()) header = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", "Authorization":"Basic " + token.decode("utf-8") } r = requests.post(BASE_URL + "/media", headers=header, files=media) print(r) if r.status_code >= 400: pprint(r.json())
When using Python 3.7 on Windows, everything works as expected: I get a
<Response [201]>
reply and I can see the image in my site’s media library.When running the exact same script on a Mac, it fails with a 503 reply (he server is temporarily busy, try again later!) from the WP server:
<Response [503]> <!DOCTYPE html> <html style="height:100%"> <head> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <title> 503 Service Unavailable </title></head> <body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;"> <div style="height:auto; min-height:100%; "> <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;"> <h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">503</h1> <h2 style="margin-top:20px;font-size: 30px;">Service Unavailable </h2> <p>The server is temporarily busy, try again later!</p> </div></div></body></html>
The MacOS is running Python 3.9.1
I can run the script again on Windows ten times, and it always works. I’ve searched the internets for the error and it’s usually a WP configuration error, which doesn’t seem to be the case here as the script works on Windows.
Any help is much appreciated!
- You must be logged in to reply to this topic.