Python to WordPress REST API remote posting 406 error even I'm passing user agent in the header

123 Views Asked by At

The following codes are used to post on WordPress site remotely. This is a python script to post on WordPress remotely. It's working on other websites. But sometimes, I'm facing issues. What should I do when I get 406 error code?

import base64
import requests
import json

# Authenticate
user = 'wp_user_name'
password = 'gikI 1g3Q cl4Q z6qG dzcs 12345'
url = 'https://sitename.com/wp-json/wp/v2'
token = base64.standard_b64encode((user + ':' + password).encode('utf-8'))
headers = {'Authorization': 'Basic ' + token.decode('utf-8'),
                        "User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
wp_title = "This is Sample Title"
slug = "this-is-sample-url"
status = 'publish'
content = '<p>This is sample paragraph</p>'

post = {'title': wp_title,
        'slug': slug,
        'status': status,
        'content': content,
        'categories': '1',
        'author': '1',
        'format': 'standard',

        }
r = requests.post(url + '/posts', headers=headers, json=post)
print(r)
0

There are 0 best solutions below