Rest API Status returned 200 but no post was made

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)

1 Comment
  1. Have you tried adding “Content-Type”: “application/json” to the header?

    ‘categories’ : [5]

    print(blog.json())

    Also, remove the date.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer