Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API question #2

Open
raybradley opened this issue May 23, 2018 · 3 comments

Comments

@raybradley
Copy link

commented May 23, 2018

I've got a mostly-functional native iOS app written in Swift. I'm hitting my head against a wall trying to PUT changes to posts with tags. Specifically, PUTs with exactly one tag work; PUTs with >1 tag fail with 500 errors. I've tried tracing the Ember client, looking at the Android code, and extensive Postman experiments. Details follow. Ideas?

Error

 errors =     (
                {
            context =             {
                key = tags;
                method = setBelongsToMany;
                tableName = posts;
            };
            errorType = BookshelfRelationsError;
            message = "Unable to update nested relation.";
        }
    );

This JSON payload works:

["posts": [["mobiledoc": "{  \"version\": \"0.3.1\",  \"markups\": [],  \"atoms\": [],  \"cards\": [    [\"card-markdown\", {      \"cardName\": \"card-markdown\",      \"markdown\": \"Link [https://itunes.apple.com/us/album/last-night-we-became-swans/1359377054]\\\\n\\\\n![](/content/images/2018/05/ed7ac36ba1504c38bb8cb6d696f4f2ed-1.jpg)\\\\n\"    }]  ],  \"sections\": [[10, 0]]}", "title": "The Summer Kills—Last Night We Became Swans", "status": "published", "id": "5ae713cfad56a57c6b4c6712", "tags": [["name": "Music"]]]]]

This JSON payload fails:

["posts": [["mobiledoc": "{  \"version\": \"0.3.1\",  \"markups\": [],  \"atoms\": [],  \"cards\": [    [\"card-markdown\", {      \"cardName\": \"card-markdown\",      \"markdown\": \"Link [https://itunes.apple.com/us/album/last-night-we-became-swans/1359377054]\\\\n\\\\n![](/content/images/2018/05/ed7ac36ba1504c38bb8cb6d696f4f2ed-1.jpg)\\\\n\"    }]  ],  \"sections\": [[10, 0]]}", "title": "The Summer Kills—Last Night We Became Swans", "status": "published", "id": "5ae713cfad56a57c6b4c6712", "tags": [["name": "Music"], ["name": "Feed"]]]]]
@vickychijwani

This comment has been minimized.

Copy link
Member

commented May 23, 2018

Hey @raybradley 👋. I may be able to help.

Your JSON payload looks strange. For example, here's a truncated version of the request body that I extracted just now from my browser's devtools for the PUT /posts/<id> call (this is Ghost v1.20.3 but I also checked with v1.22):

{
  "posts": [{
    "title": "a fancy title",
    "mobiledoc": "{\"version\":\"0.3.1\",\"markups\":[],\"atoms\":[],\"cards\":[[\"card-markdown\",{\"cardName\":\"card-markdown\",\"markdown\":\"post body text goes here\"}]],\"sections\":[[10,0]]}",
    "status": "draft",
    "tags": [{
      "name": "tag1",
      // more fields...
    },
    {
      "name": "tag2",
      // more fields...
    }],
    // more fields...
  }]
}

As you can see, the tags aren't supposed to be embedded in the mobiledoc. The mobiledoc is purely for the post body, while the title, tags, and other post settings are separate top-level fields in the post object.

For the mobiledoc format of posts, since Ghost Android works purely with markdown, currently it just treats the format as a markdown container without any semantic understanding. You can see that mobiledoc <=> markdown conversion happening here.

Hope this helps!

@raybradley

This comment has been minimized.

Copy link
Author

commented May 27, 2018

Hi, @vickychijwani ...thanks for pitching in here. I've spent a fair amount of time in your Android code (very nice, BTW :)

Your example has "more fields..." in the Tag values. One thing that's unclear is what's the bare minimum Tag representation that'll work? Which fields are necessary? I've seen the API accept just the tag name.

It's a little hard to see in my examples (Swift uses square brackets for both arrays and hashes for maximum unreadability), but my "tags" array does, indeed, lie outside the mobiledoc. Here's a slightly better representation of the failing payload:

["posts":
  [ // start of Posts array
    [ // start of first Post hash
      "mobiledoc": "{  \"version\": \"0.3.1\",  \"markups\": [],  \"atoms\": [],  \"cards\": [    [\"card-markdown\", {      \"cardName\": \"card-markdown\",      \"markdown\": \"Link [https://itunes.apple.com/us/album/last-night-we-became-swans/1359377054]\\\\n\\\\n![](/content/images/2018/05/ed7ac36ba1504c38bb8cb6d696f4f2ed-1.jpg)\\\\n\"    }]],  \"sections\": [[10, 0]]}",
      "title": "The Summer Kills—Last Night We Became Swans",
      "status": "published",
      "id": "5ae713cfad56a57c6b4c6712",
      "tags":
      [ // start of tags array
        ["name": "Music"],
        ["name": "Feed"]
      ]
    ]
  ]
]

Being that I can make this payload work in Postman but not in iOS via Alamofire, I'm starting to think the problem lies someone in how the request is getting formed.

Thanks, Vicky.

@vickychijwani

This comment has been minimized.

Copy link
Member

commented May 27, 2018

One thing that's unclear is what's the bare minimum Tag representation that'll work? Which fields are necessary? I've seen the API accept just the tag name.

You can see the minimum set of fields the Android app works with in the TagStub and PostStub classes. That's what I've found via trial and error. So yes, the API will indeed accept a tag with just the name field.

I'm starting to think the problem lies someone in how the request is getting formed

Yeah, it could be that the conversion from your Swift data structure to JSON is going wrong somewhere. It may be helpful to look at the serialized JSON sent over the wire in this case 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.