Python script to generate blogs posts issue

IslanderMark

Junior Member
Joined
Sep 2, 2020
Messages
101
Reaction score
24
if anyone can maybe help with this issue I'm encountering when running my python script that would be great, I feel like I have tried everything at this point.

I am simply trying to do some very basic posting to my site, it used to work but then I decided to switch to a VPS and now it wont work.

When I run it I get a 200 status code, I will include the output below the code. But the code does nothing, it doesn't actually post the post, it just prints out the 200 code and that it.

I also was using Wordpress xmlrpc but stopped with that cause I got an annoying error of 301 moved permanently and cant figure out that either, so if you have insight on that too i would go back to that if I can get it fixed

But the code I am trying now is as such, any info would be appreciated:

import requests
import json

# Set the endpoint URL of your WordPress website
url = 'https://MYSITE.com/wp-json/wp/v2/posts/'

# Set the credentials for authentication
username = 'USR'
password = 'PASS'

# Set the post content
title = 'Test post'
content = 'This is the content of my new post.'

# Set the request headers
headers = {
'Content-Type': 'application/json'
}

# Set the request data
data = {
'title': title,
'content': content,
'status': 'draft'
}

# Send the request
response = requests.post(url, json=data, headers=headers, auth=(username, password))

print (response.headers)
print (response.text)

# Check the response
if response.status_code == 201:
print('Post created successfully!')
else:
print('Error creating the post. Status Code:', response.status_code)
print('Error message:', response.text)

Output:

[]
Error creating the post. Status Code: 200
Error message: []
 
Last edited:
i am not an expert to help you,but why dont you try chatgpt,to answer your question,i saw many youtube videos where you post the code and it analyse it and suggests you some options
 
Status Code 200 is successful response.Check the data variable you're sending in the request is formatted correctly and check title, content, status match the expected names in the WordPress API.
 
I also use python to process automatic posts to WordPress. from my experience, the Basic-Auth-master plugin must be installed on the wordpress section.

Here's my code example:

Python:
def WP_Poster(self, w, u, p, t, c, d):     
        url = w+'/wp-json/wp/v2/posts/'
        aut = base64.b64encode(str.encode(u+':'+p)).decode('ascii')
        hd = {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Basic '+aut,}
        data = {
                'type': 'post',
                'title': t,           
                'content': c,
                'date': d,
                'status': 'publish',
        }
        r = requests.post(url, data = json.dumps(data), headers=hd)
        return r

Hope it helps
 
if anyone can maybe help with this issue I'm encountering when running my python script that would be great, I feel like I have tried everything at this point.

I am simply trying to do some very basic posting to my site, it used to work but then I decided to switch to a VPS and now it wont work.

When I run it I get a 200 status code, I will include the output below the code. But the code does nothing, it doesn't actually post the post, it just prints out the 200 code and that it.

I also was using Wordpress xmlrpc but stopped with that cause I got an annoying error of 301 moved permanently and cant figure out that either, so if you have insight on that too i would go back to that if I can get it fixed

But the code I am trying now is as such, any info would be appreciated:



Output:
Are you sure it's an error? Your code checks for 201, but 200 status is a success status as well. I am not 100% sure what wp responds with (and it's not clear from the doc as well lol), but do check the posts table and see if the post actually got inserted. If it did, just check for 200 instead of 201.
 
Here is how I do it with xmlrpc:

Python:
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressPost

client = Client('https://yourdomain/xmlrpc.php', 'user', 'password')
post = WordPressPost()
post.title = "Test Title"
post.content = "How to create post remotely"
post.id = client.call(posts.NewPost(post))
post.post_status = 'publish'
client.call(posts.EditPost(post.id, post))

If it is working on your computer but not on on VPS, it could be some network/firewall settings that is stopping you from posting. Also note that some Wordpress plugins or themes can have some added security that blocks xmlrpc( as it is a security vulnerability)
 
i am not an expert to help you,but why dont you try chatgpt,to answer your question,i saw many youtube videos where you post the code and it analyse it and suggests you some options
I have been, GPT keeps me going in circles. It has helped me with everything else along the way but it cant figure this out for me.
 
Status Code 200 is successful response.Check the data variable you're sending in the request is formatted correctly and check title, content, status match the expected names in the WordPress API.
I actually posted the header variable from the code and it seems to only allow GET requests not POST, which now is a new issue haha
 
I also use python to process automatic posts to WordPress. from my experience, the Basic-Auth-master plugin must be installed on the wordpress section.

Here's my code example:

Python:
def WP_Poster(self, w, u, p, t, c, d):    
        url = w+'/wp-json/wp/v2/posts/'
        aut = base64.b64encode(str.encode(u+':'+p)).decode('ascii')
        hd = {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Basic '+aut,}
        data = {
                'type': 'post',
                'title': t,          
                'content': c,
                'date': d,
                'status': 'publish',
        }
        r = requests.post(url, data = json.dumps(data), headers=hd)
        return r

Hope it helps
Appreciate the input, you can set the authentication app password int he suers section now
 
Are you sure it's an error? Your code checks for 201, but 200 status is a success status as well. I am not 100% sure what wp responds with (and it's not clear from the doc as well lol), but do check the posts table and see if the post actually got inserted. If it did, just check for 200 instead of 201.
I never thought to look at the posts table.

What i did notice is that the post I am trying to make is not being uploaded, but the old dummy posts are still there as well. Even though I deleted them a while ago. Not sure if this is normal or not, but if i deleted them from the site should they still be there in the table?
 
What i did notice is that the post I am trying to make is not being uploaded, but the old dummy posts are still there as well. Even though I deleted them a while ago. Not sure if this is normal or not, but if i deleted them from the site should they still be there in the table?
I think the deleted posts will get status = trash? Heck I gotta use wp again for something soon lol. See the post status column and check if it still has status = publish. If it does, you are checking/connecting to the wrong db.
 
Last edited:
Back
Top