Hello,
I’m trying to update my post directly with the Ghost API, but that does not work as expected.
Here is my code, at the end I got a http status code 200 but the content of the post is not updated.
def put_update_post(id_post=None, title=None, old_post=None, updated_post=None, oldTitle=None, newTitle="MyTest", body=None, bodyFormat='html', excerpt = None, tags=None, authors=None, status='draft', featured=False, featureImage=None):
print("put_update_post")
print("id_post : {}".format(id_post))
date_ = str((datetime.datetime.now(timezone.utc)).strftime('%Y-%m-%dT%H:%M:%S.000Z'))
if id_post == None and title != None:
id_post = search_post_id_from_title(title)
elif id_post == None and title == None:
return -1
else:
content = {"posts": [{"title": newTitle}]}
content["posts"][0]['html'] = json.dumps("<p> HELLO </p>")
content["posts"][0]['authors'] = [{ "id": "1" }]
content["posts"][0]['status'] = "published"
content["posts"][0]['featured'] = False
content["posts"][0]['updated_at'] = str(date_)
content["posts"][0]['tags'] = [{ "name": "Blue Team", "description": "blue-team" }]
url=config.ghost_admin_url + "posts/" + str(id_post) + "/"
print(url)
print(headers)
print(content)
response = requests.put(url=url, headers=headers, json=content)
if str(response.status_code)[0] == '2':
print(response.status_code)
return response.json()
else:
print(response.status_code)
return -1
The result of the code in the console :
put_update_post
id_post : 6493f7xxxxxx
https://xxxxxx.ghost.io/ghost/api/admin/posts/6493f7xxxxxx/
{'Access-Control-Allow-Methods': 'PUT', 'Content-Type': 'application/json;charset=utf-8', 'Authorization': 'Ghost xxxx'}
{'posts': [{'title': 'TEST TEST', 'html': '"<p> HELLO </p>"', 'authors': [{'id': '1'}], 'status': 'published', 'featured': False, 'updated_at': '2023-06-22T08:10:40.000Z', 'tags': [{'name': 'Blue Team', 'description': 'blue-team'}]}]}
200
Thank you in advance.