# Unable to update post through API (Python)

**URL:** <https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370>\
**Category:** Integrations & API\
**Created:** [June 22, 2023, 8:16am UTC](https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370 "2023-06-22T08:16:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_B](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/julien_b/32/25062_2.png) [@Julien\_B](https://forum.ghost.org/u/Julien_B)\
**Post date:** [June 22, 2023, 8:16am UTC](https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370/1 "2023-06-22T08:16:50Z")

</div>

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.

```auto
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 :

```auto
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.

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [June 22, 2023, 12:11pm UTC](https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370/2 "2023-06-22T12:11:19Z")

</div>

If you’re doing a PUT, you have to pass an updated\_at that matches what’s already on the post, not the current time.

---

<div class="post-metadata">

**Author:** ![Julien\_B](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/julien_b/32/25062_2.png) [@Julien\_B](https://forum.ghost.org/u/Julien_B)\
**Post date:** [June 22, 2023, 1:55pm UTC](https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370/3 "2023-06-22T13:55:36Z")

</div>

Hello, thank you but it is not the issue.

I just fixed it by adding

```auto
content['mobiledoc'] = json.dumps({ 'version': '0.3.1', 'markups': [], 'atoms': [], 'cards': [['html', {'cardName': 'html', 'html': body}]], 'sections': [[10, 0]]});

```

Just before:

```auto
content["posts"][0]['html'] = json.dumps("<p> HELLO </p>")

```

Best regards

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [June 22, 2023, 2:55pm UTC](https://forum.ghost.org/t/unable-to-update-post-through-api-python/39370/4 "2023-06-22T14:55:58Z")

</div>

Oh good, glad you figured it out! :) Embedding HTML in a card is definitely one option. You can also pass HTML without putting it into a card, if it’s simple enough HTML that the HTML parser can understand it. Or at least you could a couple versions ago - with the new lexical editor, I’m not 100% sure if it still works the way I’ve done it.
