I’m trying to make a test post using the REST API and I get a 200 response for authentication, assuming that’s good. and now a 200 response for the post, but in my searching, it appears a 201 is what you need to show that a post was created. Double checked and no post was created on my site so obviously, there is an issue somewhere.
The code I’m using is below if anyone can help me out. Trying to get the basic test working before I start modifying it for what I actually need, hopefully, if there are errors at that point I can better narrow it down when I had a working base version to begin with. Personal information redacted of course, I am using the WordPress user applications passwords as well.
Any advice is appreciated.
import requests
import json
import base64
username = “\*\*\*”
password = “\*\*\*\*”
creds = username + ‘:’ + password
cred\_token = base64.b64encode(creds.encode())
header = {‘Authorization’: ‘Basic ‘ + cred\_token.decode(‘utf-8’)}
url = “http://\*\*\*.com/wordpress/wp-json/wp/v2”
post = {
‘title’ : ‘Testing’,
‘content’ : ‘Hello, this is a test’,
‘status’ : ‘publish’,
‘categories’: 5,
‘date’ : ‘2023-01-26T11:00:00’
}
blog = requests.post(url + ‘/posts’ , headers=header , json=post)
print(blog)
Have you tried adding “Content-Type”: “application/json” to the header?
‘categories’ : [5]
print(blog.json())
Also, remove the date.