The problem is with your POST
request, where the content
property should be a plain string and not an object. You should also send just the data that you want to change.
When updating a Page (i.e. a post of the type page
), you should provide a JSON-encoded or URL-encoded string containing one or more of the properties/parameters as listed here.
Examples using cURL from the command line prompt:
-
URL-encoded (
Content-Type
header isapplication/x-www-form-urlencoded
):curl -d "title=New%20Title!&content=New%20Content%3F" -X POST https://example.com/wp-json/wp/v2/pages/9 --user "admin:QUDM PYFn xfyK 9Hhe Ayf2 hk6k"
-
JSON-encoded
curl -H "Content-Type: application/json" -d "{\"title\":\"New Title!\",\"content\":\"New Content?\"}" -X POST https://example.com/wp-json/wp/v2/pages/9 --user "admin:QUDM PYFn xfyK 9Hhe Ayf2 hk6k"
So in your code:
-
This line:
tmp['content']['rendered'] = newPageContent
-
should be:
tmp = { 'content': newPageContent }
but you can of course add other properties that you want to change; e.g.
title
andstatus
.
And (despite that I’m not a Python expert), you can use the example below as a reference when creating/retrieving/updating/deleting a Page:
import base64, json, requests
user="admin"
pythonapp = 'QUDM PYFn xfyK 9Hhe Ayf2 hk6k'
url="https://example.com/wp-json/wp/v2"
data_string = user + ':' + pythonapp
token = base64.b64encode( data_string.encode() )
headers = { 'Authorization': 'Basic ' + token.decode( 'utf8' ) }
# Create a Page.
data = { 'title': 'Testing from Python' }
r = requests.post( url + '/pages/', headers=headers, json=data )
data = r.json()
page_id = str( data['id'] )
print 'Page created. ID: ' + page_id
# Retrieve the Page.
r = requests.get( url + '/pages/' + page_id, headers=headers )
data = r.json()
title = data['title']['rendered']
print 'Page #' + page_id + ' retrieved. The title is: ' + title
# Update the Page.
data = { 'content': 'New content here' }
r = requests.post( url + '/pages/' + page_id, headers=headers, json=data )
data = r.json()
content = data['content']['rendered']
print 'Page #' + page_id + ' updated. Content set to: ' + content
# Delete the Page.
r = requests.delete( url + '/pages/' + page_id, headers=headers )
data = r.json()
print 'Page #' + page_id + ' moved to the Trash. (Well, it should be...)'
print 'The Page "status" is ' + data['status']