I’ve not seen any recent API changes that would impact the code you provided, and tested it myself on a fresh site. Most common errors, such as authentication issues, are covered under other error responses.
Given the 400 error, I recommend checking your variables, in case they are leading to a malformed request, and also taking a look at any possible server/cache/CDN changes.
If you’re still stuck, check back with more details about your situation. What version of WordPress are you updating from? Are all of these sites hosted in the same place?
I’m seeing the same issue, but I don’t have any authentication. Just a GET request that works in the browser but not in Python requests library
For me it seemed the issue was specific to the “requests” library and the wordpress API. Using urllib3 instead for my requests worked.
@tavy87 thank you, since I’m not familiar with urllib3, any idea how to convert my code into urllib3 instead of requests?
I tried to do so, but I kept getting errors
Try this after reusing some of your variables:
import urllib3
import json
http = urllib3.PoolManager()
encoded_data = json.dumps(post).encode(‘utf-8’)
response = http.request(‘POST’, url + postid, body=encoded_data, headers=header)
response_json = json.loads(response.data.decode(‘utf-8’))
Thank you very much, I tried it but for some reason it doesn’t update the post, as requests used to do! I noticed that I started getting response code 200, instead of 201 that I used to get.
By the way, I found out why my original code stopped working, its because the hosting “SiteGround” started blocking requests with user agent “python-requests”.
I tried setting the user-agent manually:
postcontent = {
"content" : content,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
but for some reason it doesn’t really change, when I print the response I can see that the user-agent still python-requests:
837 <Response [400]> b'400 - Bad Request | Your browser sent a request this server could not understand.\n'
https://www.example.com/wp-json/wp/v2/pages/837
{'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Basic user: pass', 'Content-Length': '12023', 'Content-Type': 'application/json'}
-
This reply was modified 1 year, 2 months ago by
Tadawoul.
anyone solve this?
i’m having it at cloudways. works php cloudways server code to another one, but not from my desktop in python.
it is returning what looks like method=get instead of posting anything