Reputation: 21
The WordPress Rest API returns a 406 error when attempting to post with the following python script. I've stripped the JSON post data down to a bare minimum and used the WordPress core application password generator with several different user accounts all which have administrator privileges. I've tried using the password with the spaces included and with them removed as well to no avail. I've followed a tutorial on YouTube and modeled my script to match the tutor's with both using https for SSL and without. using http, also to no avail. I would really appreciate any help to resolve this issue: Here's the script I used:
import os
import json
import requests
import base64
url = 'https://domainname.com/wp-json/wp/v2'
user = 'AdministratorUserName'
password = '0123 4567 8901 2345 6789 0123'
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = {
'date': '2022-03-23T10:00:00',
'title': 'testing',
'content': 'testing testing 123',
'status': 'publish'
}
wordPressresponse = requests.post(url + '/posts', headers=header, json=post)
with open('wordPresResponseLog.txt', 'a') as wPResponse:
wPResponse.write(str(wordPressresponse))
Upvotes: 0
Views: 792
Reputation: 21
I was able to resolve the issue by adding the following line to the header:
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
Upvotes: 2