Python, requests is the library that replaces curl in python.
Response object from get request:
{‘posts’: [{‘id’: ‘664c1abf57f55790747b8caf’, ‘uuid’: ‘098d09c5-1e2f-4d26-9198-5e95d86b3617’, ‘title’: ‘Ark’, ‘slug’: ‘ark’, ‘mobiledoc’: None, ‘lexical’: ‘{“root”:{“children”:[{“type”:“markdown”,“version”:1,“markdown”:“#Please welcome Ark\n###Black Mouth Cur / Mixed (short coat)\nPlease welcome Ark\xa0to WWR! This southern mystery boy went from an urgent euthanasia list to arriving in style by plane and has been aged at 7\xa0months. Ark has been fostered with other dogs and has acclimated well with proper introduction and bonds quickly. Ark is a confident pup and requires an experienced handler who will continue training with Green Mountain K9, Paws in Motion, or Radford K9 specifically. The adoption fee is 600 and he is currently on hold for adoptions at this time.\xa0 If you are interested in adopting, please fill out an application.\n***\n### More about Ark\n- Not good with cats\n- Good with dogs\n- Not good with children\n- Reaction to new people: Aggressive\n- House trained\n- Color: Tricolor (Tan/Brown & Black & White)\n- Activity level: Moderately Active\n- Has Basic Training\n- Indoor/outdoor: Indoor and Outdoor\n\n\n\n\n\n\n\n\n”}],“direction”:null,“format”:“”,“indent”:0,“type”:“root”,“version”:1}}’, ‘comment_id’: ‘664c1abf57f55790747b8caf’, ‘feature_image’: ‘https://cdn.[api].org/8409/pictures/animals/19650/19650207/96768655.jpg’, ‘featured’: False, ‘status’: ‘published’, ‘visibility’: ‘public’, ‘created_at’: ‘2024-05-21T03:53:35.000Z’, ‘updated_at’: ‘2024-05-22T02:26:43.000Z’, ‘published_at’: ‘2024-05-21T03:53:35.000Z’, ‘custom_excerpt’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘custom_template’: None, ‘canonical_url’: None, ‘tags’: [{‘id’: ‘66496ed488f6e604b8dda029’, ‘name’: ‘Adopted’, ‘slug’: ‘adopted’, ‘description’: ‘Our rescue hall of fame, dogs which have been adopted thru our rescue.’, ‘feature_image’: None, ‘visibility’: ‘public’, ‘og_image’: None, ‘og_title’: None, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘canonical_url’: None, ‘accent_color’: ‘#7a7a7a’, ‘created_at’: ‘2024-05-19T03:15:32.000Z’, ‘updated_at’: ‘2024-05-19T04:29:12.000Z’, ‘url’: ‘http://www.[ghost_domain].org/tag/adopted/’}], ‘authors’: [{‘id’: ‘1’, ‘name’: ‘Chris name’, ‘slug’: ‘chris’, ‘email’: ‘email@gmail.com’, ‘profile_image’: None, ‘cover_image’: None, ‘bio’: None, ‘website’: None, ‘location’: None, ‘facebook’: None, ‘twitter’: None, ‘accessibility’: ‘{“nightShift”:false}’, ‘status’: ‘active’, ‘meta_title’: None, ‘meta_description’: None, ‘tour’: None, ‘last_seen’: ‘2024-05-23T14:22:51.000Z’, ‘comment_notifications’: True, ‘free_member_signup_notification’: True, ‘paid_subscription_started_notification’: True, ‘paid_subscription_canceled_notification’: False, ‘mention_notifications’: True, ‘recommendation_notifications’: True, ‘milestone_notifications’: True, ‘donation_notifications’: True, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-05-23T14:22:51.000Z’, ‘roles’: [{‘id’: ‘660d6421edf285469a44b782’, ‘name’: ‘Owner’, ‘description’: ‘Blog Owner’, ‘created_at’: ‘2024-04-03T14:13:53.000Z’, ‘updated_at’: ‘2024-04-03T14:13:53.000Z’}], ‘url’: ‘http://www.[ghost_domain].org/author/chris/’}], ‘tiers’: [{‘id’: ‘660d6422edf285469a44b78b’, ‘name’: ‘Free’, ‘slug’: ‘free’, ‘active’: True, ‘welcome_page_url’: None, ‘visibility’: ‘public’, ‘trial_days’: 0, ‘description’: ‘See Weekly Updates from Wags!’, ‘type’: ‘free’, ‘currency’: None, ‘monthly_price’: None, ‘yearly_price’: None, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-04-03T18:54:53.000Z’, ‘monthly_price_id’: None, ‘yearly_price_id’: None}, {‘id’: ‘660d6422edf285469a44b78c’, ‘name’: ‘Wags n Rescue’, ‘slug’: ‘default-product’, ‘active’: True, ‘welcome_page_url’: None, ‘visibility’: ‘public’, ‘trial_days’: 0, ‘description’: None, ‘type’: ‘paid’, ‘currency’: ‘usd’, ‘monthly_price’: 500, ‘yearly_price’: 5000, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-04-03T14:17:57.000Z’, ‘monthly_price_id’: None, ‘yearly_price_id’: None}], ‘count’: {‘clicks’: 0, ‘positive_feedback’: 0, ‘negative_feedback’: 0}, ‘primary_author’: {‘id’: ‘1’, ‘name’: ‘Chris name’, ‘slug’: ‘chris’, ‘email’: ‘email@gmail.com’, ‘profile_image’: None, ‘cover_image’: None, ‘bio’: None, ‘website’: None, ‘location’: None, ‘facebook’: None, ‘twitter’: None, ‘accessibility’: ‘{“nightShift”:false}’, ‘status’: ‘active’, ‘meta_title’: None, ‘meta_description’: None, ‘tour’: None, ‘last_seen’: ‘2024-05-23T14:22:51.000Z’, ‘comment_notifications’: True, ‘free_member_signup_notification’: True, ‘paid_subscription_started_notification’: True, ‘paid_subscription_canceled_notification’: False, ‘mention_notifications’: True, ‘recommendation_notifications’: True, ‘milestone_notifications’: True, ‘donation_notifications’: True, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-05-23T14:22:51.000Z’, ‘roles’: [{‘id’: ‘660d6421edf285469a44b782’, ‘name’: ‘Owner’, ‘description’: ‘Blog Owner’, ‘created_at’: ‘2024-04-03T14:13:53.000Z’, ‘updated_at’: ‘2024-04-03T14:13:53.000Z’}], ‘url’: ‘http://www.[ghost_domain].org/author/chris/’}, ‘primary_tag’: {‘id’: ‘66496ed488f6e604b8dda029’, ‘name’: ‘Adopted’, ‘slug’: ‘adopted’, ‘description’: ‘Our rescue hall of fame, dogs which have been adopted thru our rescue.’, ‘feature_image’: None, ‘visibility’: ‘public’, ‘og_image’: None, ‘og_title’: None, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘canonical_url’: None, ‘accent_color’: ‘#7a7a7a’, ‘created_at’: ‘2024-05-19T03:15:32.000Z’, ‘updated_at’: ‘2024-05-19T04:29:12.000Z’, ‘url’: ‘http://www.[ghost_domain].org/tag/adopted/’}, ‘email_segment’: ‘all’, ‘url’: ‘http://www.[ghost_domain].org/ark/’, ‘excerpt’: ‘Please welcome Ark\n\n\n\nBlack Mouth Cur / Mixed (short coat)\n\n\nPlease welcome Ark\xa0to WWR! This southern mystery boy went from an urgent euthanasia list to arriving in style by plane and has been aged at 7\xa0months. Ark has been fostered with other dogs and has acclimated well with proper introduction and bonds quickly. Ark is a confident pup and requires an experienced handler who will continue training with Green Mountain K9, Paws in Motion, or Radford K9 specifically. The adoption fee is 600 and h’, ‘reading_time’: 1, ‘og_image’: ‘https://cdn.[api].org/8409/pictures/animals/19650/19650207/96768655.jpg’, ‘og_title’: ‘Please welcome Ark’, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘email_subject’: None, ‘frontmatter’: None, ‘feature_image_alt’: None, ‘feature_image_caption’: None, ‘email_only’: False, ‘email’: None, ‘newsletter’: None}]}
Put object, two fields were updated, posts/tags/name, and post/tags/slug.
{‘posts’: [{‘id’: ‘664c1abf57f55790747b8caf’, ‘uuid’: ‘098d09c5-1e2f-4d26-9198-5e95d86b3617’, ‘title’: ‘Ark’, ‘slug’: ‘ark’, ‘mobiledoc’: None, ‘lexical’: ‘{“root”:{“children”:[{“type”:“markdown”,“version”:1,“markdown”:“#Please welcome Ark\n###Black Mouth Cur / Mixed (short coat)\nPlease welcome Ark\xa0to WWR! This southern mystery boy went from an urgent euthanasia list to arriving in style by plane and has been aged at 7\xa0months. Ark has been fostered with other dogs and has acclimated well with proper introduction and bonds quickly. Ark is a confident pup and requires an experienced handler who will continue training with Green Mountain K9, Paws in Motion, or Radford K9 specifically. The adoption fee is 600 and he is currently on hold for adoptions at this time.\xa0 If you are interested in adopting, please fill out an application.\n***\n### More about Ark\n- Not good with cats\n- Good with dogs\n- Not good with children\n- Reaction to new people: Aggressive\n- House trained\n- Color: Tricolor (Tan/Brown & Black & White)\n- Activity level: Moderately Active\n- Has Basic Training\n- Indoor/outdoor: Indoor and Outdoor\n\n\n\n\n\n\n\n\n”}],“direction”:null,“format”:“”,“indent”:0,“type”:“root”,“version”:1}}’, ‘comment_id’: ‘664c1abf57f55790747b8caf’, ‘feature_image’: ‘https://cdn.[api].org/8409/pictures/animals/19650/19650207/96768655.jpg’, ‘featured’: False, ‘status’: ‘published’, ‘visibility’: ‘public’, ‘created_at’: ‘2024-05-21T03:53:35.000Z’, ‘updated_at’: ‘2024-05-22T02:26:43.000Z’, ‘published_at’: ‘2024-05-21T03:53:35.000Z’, ‘custom_excerpt’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘custom_template’: None, ‘canonical_url’: None, ‘tags’: [{‘id’: ‘66496ed488f6e604b8dda029’, ‘name’: ‘Adoptable’, ‘slug’: ‘adoptable’, ‘description’: ‘Our rescue hall of fame, dogs which have been adopted thru our rescue.’, ‘feature_image’: None, ‘visibility’: ‘public’, ‘og_image’: None, ‘og_title’: None, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘canonical_url’: None, ‘accent_color’: ‘#7a7a7a’, ‘created_at’: ‘2024-05-19T03:15:32.000Z’, ‘updated_at’: ‘2024-05-19T04:29:12.000Z’, ‘url’: ‘http://www.[ghost_domain].org/tag/adopted/’}], ‘authors’: [{‘id’: ‘1’, ‘name’: ‘Chris name’, ‘slug’: ‘chris’, ‘email’: ‘email@gmail.com’, ‘profile_image’: None, ‘cover_image’: None, ‘bio’: None, ‘website’: None, ‘location’: None, ‘facebook’: None, ‘twitter’: None, ‘accessibility’: ‘{“nightShift”:false}’, ‘status’: ‘active’, ‘meta_title’: None, ‘meta_description’: None, ‘tour’: None, ‘last_seen’: ‘2024-05-23T14:22:51.000Z’, ‘comment_notifications’: True, ‘free_member_signup_notification’: True, ‘paid_subscription_started_notification’: True, ‘paid_subscription_canceled_notification’: False, ‘mention_notifications’: True, ‘recommendation_notifications’: True, ‘milestone_notifications’: True, ‘donation_notifications’: True, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-05-23T14:22:51.000Z’, ‘roles’: [{‘id’: ‘660d6421edf285469a44b782’, ‘name’: ‘Owner’, ‘description’: ‘Blog Owner’, ‘created_at’: ‘2024-04-03T14:13:53.000Z’, ‘updated_at’: ‘2024-04-03T14:13:53.000Z’}], ‘url’: ‘http://www.[ghost_domain].org/author/chris/’}], ‘tiers’: [{‘id’: ‘660d6422edf285469a44b78b’, ‘name’: ‘Free’, ‘slug’: ‘free’, ‘active’: True, ‘welcome_page_url’: None, ‘visibility’: ‘public’, ‘trial_days’: 0, ‘description’: ‘See Weekly Updates from Wags!’, ‘type’: ‘free’, ‘currency’: None, ‘monthly_price’: None, ‘yearly_price’: None, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-04-03T18:54:53.000Z’, ‘monthly_price_id’: None, ‘yearly_price_id’: None}, {‘id’: ‘660d6422edf285469a44b78c’, ‘name’: ‘Wags n Rescue’, ‘slug’: ‘default-product’, ‘active’: True, ‘welcome_page_url’: None, ‘visibility’: ‘public’, ‘trial_days’: 0, ‘description’: None, ‘type’: ‘paid’, ‘currency’: ‘usd’, ‘monthly_price’: 500, ‘yearly_price’: 5000, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-04-03T14:17:57.000Z’, ‘monthly_price_id’: None, ‘yearly_price_id’: None}], ‘count’: {‘clicks’: 0, ‘positive_feedback’: 0, ‘negative_feedback’: 0}, ‘primary_author’: {‘id’: ‘1’, ‘name’: ‘Chris name’, ‘slug’: ‘chris’, ‘email’: ‘email@gmail.com’, ‘profile_image’: None, ‘cover_image’: None, ‘bio’: None, ‘website’: None, ‘location’: None, ‘facebook’: None, ‘twitter’: None, ‘accessibility’: ‘{“nightShift”:false}’, ‘status’: ‘active’, ‘meta_title’: None, ‘meta_description’: None, ‘tour’: None, ‘last_seen’: ‘2024-05-23T14:22:51.000Z’, ‘comment_notifications’: True, ‘free_member_signup_notification’: True, ‘paid_subscription_started_notification’: True, ‘paid_subscription_canceled_notification’: False, ‘mention_notifications’: True, ‘recommendation_notifications’: True, ‘milestone_notifications’: True, ‘donation_notifications’: True, ‘created_at’: ‘2024-04-03T14:13:54.000Z’, ‘updated_at’: ‘2024-05-23T14:22:51.000Z’, ‘roles’: [{‘id’: ‘660d6421edf285469a44b782’, ‘name’: ‘Owner’, ‘description’: ‘Blog Owner’, ‘created_at’: ‘2024-04-03T14:13:53.000Z’, ‘updated_at’: ‘2024-04-03T14:13:53.000Z’}], ‘url’: ‘http://www.[ghost_domain].org/author/chris/’}, ‘primary_tag’: {‘id’: ‘66496ed488f6e604b8dda029’, ‘name’: ‘Adopted’, ‘slug’: ‘adopted’, ‘description’: ‘Our rescue hall of fame, dogs which have been adopted thru our rescue.’, ‘feature_image’: None, ‘visibility’: ‘public’, ‘og_image’: None, ‘og_title’: None, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘codeinjection_head’: None, ‘codeinjection_foot’: None, ‘canonical_url’: None, ‘accent_color’: ‘#7a7a7a’, ‘created_at’: ‘2024-05-19T03:15:32.000Z’, ‘updated_at’: ‘2024-05-19T04:29:12.000Z’, ‘url’: ‘http://www.[ghost_domain].org/tag/adopted/’}, ‘email_segment’: ‘all’, ‘url’: ‘http://www.[ghost_domain].org/ark/’, ‘excerpt’: ‘Please welcome Ark\n\n\n\nBlack Mouth Cur / Mixed (short coat)\n\n\nPlease welcome Ark\xa0to WWR! This southern mystery boy went from an urgent euthanasia list to arriving in style by plane and has been aged at 7\xa0months. Ark has been fostered with other dogs and has acclimated well with proper introduction and bonds quickly. Ark is a confident pup and requires an experienced handler who will continue training with Green Mountain K9, Paws in Motion, or Radford K9 specifically. The adoption fee is 600 and h’, ‘reading_time’: 1, ‘og_image’: ‘https://cdn.[api].org/8409/pictures/animals/19650/19650207/96768655.jpg’, ‘og_title’: ‘Please welcome Ark’, ‘og_description’: None, ‘twitter_image’: None, ‘twitter_title’: None, ‘twitter_description’: None, ‘meta_title’: None, ‘meta_description’: None, ‘email_subject’: None, ‘frontmatter’: None, ‘feature_image_alt’: None, ‘feature_image_caption’: None, ‘email_only’: False, ‘email’: None, ‘newsletter’: None}]}
The put http response: (the error)
Failed to update post: b’{“errors”:[{“message”:“Validation error, cannot edit post.”,“context”:“Validation failed for posts.”,“type”:“ValidationError”,“details”:[{“keyword”:“required”,“dataPath”:“”,“schemaPath”:“#/required”,“params”:{“missingProperty”:“posts”},“message”:“should have required property 'posts'”}],“property”:“posts”,“help”:null,“code”:null,“id”:“3fc4c2e0-1912-11ef-9c9a-4f504e4b4824”,“ghostErrorCode”:null}]}’
The python function that is successfully getting the data, but unsuccessful in the post update:
def update_post_tags(token, post_id, tag):
url = f’http://www.[ghost_domain].org/ghost/api/admin/posts/{post_id}/’
print(url)
headers = {'Authorization': 'Ghost {}'.format(token), "Content-Type": "application/json;charset=utf-8"}
# Fetch the existing post data
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to fetch post: {response.content}")
getData = response.json()
print(getData)
if tag == 0:# mutate the dog to the adopted category tag
getData['posts'][0]['tags'][0]['name'] = 'Adoptable'
getData['posts'][0]['tags'][0]['slug'] = 'adoptable'
#getData['posts'][0]['tags'][0]['updated_at'] = timestamp = now.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
if tag == 1: # mutate the dog to the adoptable category tag
getData['posts'][0]['tags'][0]['name'] = 'Adoptable'
getData['posts'][0]['tags'][0]['slug'] = 'adoptable'
#getData['posts'][0]['tags'][0]['updated_at'] = timestamp = now.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
print(getData)
print("\n")
response = requests.put(url, headers=headers, json=getData)
if response.status_code == 200:
print(f"Successfully updated post tags for post ID {post_id} to {tag}.\n{response.content}")
else:
print(f"Failed to update post: {response.content}")