Unable to update posts with Admin API and html-to-mobiledoc tool

I am having troubles with adding a newly created mobiledoc segment to an existing post with a given ID. Authentication to the Admin API and the GET request (to pull out the updated_at field) work fine. Updating the title with the chained PUT request seems to work well, too.
However I am running into issues when I am trying to update the mobiledoc. I am using the html-to-mobiledoc tool https://github.com/TryGhost/Ghost-SDK/tree/master/packages/html-to-mobiledoc but I keep getting the error 422.

Can someone please tell me what I am doing wrong? I did spend a lot of time fiddling around with this issue.

This is my code:

const converter = require('@tryghost/html-to-mobiledoc');

// Create a token without the client
    const jwt = require('jsonwebtoken');
    const axios = require('axios');

// Admin API key goes here
const key = 'ADMIN-API_KEY';

// Split the key into ID and SECRET
const [id, secret] = key.split(':');

// Create the token (including decoding secret)
const token = jwt.sign({}, Buffer.from(secret, 'hex'), {
keyid: id,
algorithm: 'HS256',
expiresIn: '5m',
audience: `/v2/admin/`

// Make an authenticated request to create a post
const url = 'http://localhost:2368/ghost/api/v2/admin/posts/5db55dd518abc5390c725294';
const headers = { Authorization: `Ghost ${token}` };

//GET request
axios.get(url, { headers })
.then((response) => {
    let updated=response.data.posts[0].updated_at;
    let modoc=converter.toMobiledoc('<h1>Hello World!</h1>');
    let modoc_str = JSON.stringify(modoc)
    let payload = { posts: [{
        title: "Fresh new title",
        updated_at: updated,
        mobile_doc: modoc_str 
// PUT request
    return axios.put(url, payload,{ headers })
.then(response => console.log(response.data))
.catch(error => console.error(error));

@Ulineu the field name is mobiledoc not mobile_doc, I think that’s likely the problem.

One tip, it will help if you console.log the response content when you get a 422 error as it will have details about what went wrong such as telling you which fields are missing, unknown, or have invalid data in.


Oh, I missed that simple one. It works without errors. Thanks.
However it would be great to have the html-to-mobiledoc for Python users, too.
Are there any plans? Data manipulation and transformation as well as one-off projects are quite conveniently to do with Python.
For example, at some point earlier I was able to amend all post headers via the Selenium library at the Ghost Admin frontend. :nerd_face:
My current Projects: I need to re-insert spell-checked texts into the blog posts, and I am planning to add a better tagging structure to my blog, too.

No current plans from the core team but it’s all open source and we would welcome any contributions to our SDK and client libraries to increase support to other languages.

I’m not sure if it fits your use-case but you can push html directly to the Admin API and it will convert to mobiledoc for you https://ghost.org/docs/api/v3/admin/#source-html

1 Like