Latest posts API pagination

(Dawid Urbański) #1

Hello everyone!

I’m getting latest posts from api using this endpoint:

How can I get more results from latest posts? Is this possible?

I tried but it’s not working.

Thanks in advance!

(Jay Pfaffman) #2

When I figured that out before I followed the instructions in the “reverse engineering” topic. I thought that I’d done it in GitHub - pfaffman/discourse-downloader: Download a Discourse topic for offline analysis, but I don’t see it there.

(Blake Erickson) #3

You might have to hit /latest.json?page=2 instead of /posts.json and filter out the posts from the result.

EDIT: Ohh you want latest posts, not latest topics. Considering /posts by itself doesn’t have a UI, it probably is missing pagination, I’m sure a PR could be created for this. But to get what you want right now you could still parse the /latest.json endpoint and parse out the topic_id and the post number and fetch the post with /t/<topic_id>/<post_number>, but yea I can see why pagination on the /posts endpoint would be helpful.

(Blake Erickson) #4

Okay, now that I’ve looked at the controller for /posts:

def latest

there is a “before” parameter, not a “page” parameter, but using it like a “page” is a bit different because it filters out private posts and such. But I think roughly you can use it like this:

  • hit /posts.json and get the first post id (currently it is 22000)
  • subtract 50 (that endpoint only shows at most 50 at a time)
  • hit /posts.json?before=21950
  • repeat

Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

How to get more posts from posts.json
(Dawid Urbański) #5

Thanks guys! I’ll play with it.

(Kane York) #6

as the API designer: it works this way to make things easier on the Discourse server; if the server had to do a bunch of work to always fill up 50 posts anything using the API might start being a perf problem.

Basically it forces the consumers to be “good neighbors”, in a weird sense of the term.