rest api - Update postpage using API + python

admin2025-01-08  4

I'm trying to update posts using python and API, the issue that I keep getting code 200 but the post doesn't actually get updated!

here is my code:

import requests
import json
import base64

url ='http://www.***/wp-json/wp/v2'

username="------"
password="------"

creds = username + ':' + password
cred_token = base64.b64encode(creds.encode())

header = {'Authorization': 'Basic ' + cred_token.decode('utf-8')}


postID = "36559"
post = {
 'content' : "Hello, this content updated."
}

blog = requests.post(url + "/posts/" + postID , headers=header, json=post)
print(blog)

I also tried, but kept getting response 404:

blog = requests.post(url + "/" + postID , headers=header, json=post)

Edit - Solution: the issue was that the url had http and not https, it seems i have to use the https version, although its weird I kept getting 200 response. Now it works

I'm trying to update posts using python and API, the issue that I keep getting code 200 but the post doesn't actually get updated!

here is my code:

import requests
import json
import base64

url ='http://www.***.com/wp-json/wp/v2'

username="------"
password="------"

creds = username + ':' + password
cred_token = base64.b64encode(creds.encode())

header = {'Authorization': 'Basic ' + cred_token.decode('utf-8')}


postID = "36559"
post = {
 'content' : "Hello, this content updated."
}

blog = requests.post(url + "/posts/" + postID , headers=header, json=post)
print(blog)

I also tried, but kept getting response 404:

blog = requests.post(url + "/" + postID , headers=header, json=post)

Edit - Solution: the issue was that the url had http and not https, it seems i have to use the https version, although its weird I kept getting 200 response. Now it works

Share Improve this question edited Oct 19, 2022 at 13:24 Fadi asked Oct 19, 2022 at 10:39 FadiFadi 732 silver badges6 bronze badges 2
  • I don’t know Python, but it looks like you’re using the POST method. To update posts you need to send a PUT request. – Jacob Peattie Commented Oct 19, 2022 at 11:16
  • @JacobPeattie same issue, I get response 200, but the post doesn't change – Fadi Commented Oct 19, 2022 at 13:05
Add a comment  | 

1 Answer 1

Reset to default 0

Edit - Solution: the issue was that the url had http and not https, it seems i have to use the https version, although its weird I kept getting 200 response before.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267879a1229.html

最新回复(0)