I’m trying to create a new post using the rest API (python). My post (title, slug, etc) get created, except for the text body (formatted in HTML). I can’t figure out how to fix this. Any ideas? I tried wrapping it in: <!--kg-card-begin: html-->
, that didn’t work.
This is my code:
def create_post(title, content, slug, description, status='published'):
print(f"Creating post with title: {title}, slug: {slug}")
print(f"Post content: {content}")
token = generate_jwt_token(ghost_admin_api_key)
if not token:
print("Failed to generate JWT token. Exiting.")
return None
headers = {
'Authorization': f'Ghost {token}',
'Content-Type': 'application/json'
}
post_data = {
'posts': [
{
'title': title,
'slug': slug,
'html': content,
'status': status,
'meta_title': title,
'meta_description': description,
}
]
}
print(f"Making request to URL: {ghost_admin_api_url}posts/?source=html")
print("Request body:", post_data)
response = requests.post(f'{ghost_admin_api_url}posts/', json=post_data, headers=headers)
print(f"Response Status Code: {response.status_code}")
print(f"Response Content: {response.text}")
if response.status_code == 201:
print('Post created successfully.')
post_id = response.json()['posts'][0]['id']
print('Post ID: ', post_id)
return post_id
else:
print('Failed to create post.')
try:
error_info = response.json()
except ValueError:
error_info = {'error': 'Non-JSON response'}
print(f"Error Response: {error_info}")
return None
This is the response I'm getting. Lexical is empty.
Response Content: “mobiledoc”:null,“lexical”:"{"root":{"children":[{"children":,"direction":null,"format":"","indent":0,"type":"paragraph","version":1}]
HTML text example
<!--kg-card-begin: html-->
<h2>Introduction to English Seafood Vocabulary</h2>
<p>Seafood is an essential part of many cultures and cuisines around the world. Understanding seafood vocabulary can enrich your dining experiences and improve your English language skills. This guide will introduce you to some common terms you might encounter.</p>
<h3>Types of Seafood</h3>
<!--kg-card-end: html-->