1

Working with the Stack Exchange 2.2 API, I have run into a roadblock when using post_ids to retrieve data. My results are incomplete.

According the online documents for the posts/{ids} interface, I should be able to retrieve 30 questions. (It says up to 100, but other queries are maxing out at 30, so I assume this would as well.)

But, using this query ("Query 1"):

/2.2/posts/44697152;44725527;44740511;44740358;44746963;44744722;44752663;44751224;44750591;44768116;44768073;44766268;44762034;44763620;44763575;44763124;44778755;44782321;44792568;44791709;44791955;44801095;44800945;44808174;44809993;44801579;44838390;44872388;44853254;44850293

I get back exactly 3 entries, those for: 44850293; 44872388; and 44853254.

(lldb) po result

["items": [

["creation_date": 2017-06-30 15:41:31 +0000, "link": How to unwrap the content value from XML in Objective-C, "score": -1, "post_type": Wednesday.PostType.question, "last_activity_date": 2017-07-03 13:57:21 +0000, "owner": ["link": https://www.gravatar.com/avatar/e0616f34ea00d07842b7e7886fd7abc8?s=128&d=identicon&r=PG&f=1, "user_type": Wednesday.User.UserType.registered, "user_id": 7707593, "display_name": "Steven", "reputation": 18, "accept_rate": 75], "last_edit_date": 2017-07-03 07:41:31 +0000],

["creation_date": 2017-07-02 15:25:37 +0000, "link": How to collect the return value of a function (Swift 3), "score": -4, "post_type": Wednesday.PostType.question, "last_activity_date": 2017-07-02 15:43:58 +0000, "owner": ["link": https://lh6.googleusercontent.com/-NIYQgyduM8w/AAAAAAAAAAI/AAAAAAAAAaQ/e0IACRo6xbI/photo.jpg?sz=128, "user_type": Wednesday.User.UserType.registered, "user_id": 6298390, "display_name": "Dorian Brown", "reputation": 18, "accept_rate": 100], "last_edit_date": 2017-07-02 15:43:58 +0000],

["creation_date": 2017-06-30 19:02:07 +0000, "link": swift - approach for tangled/cross-cutting code (logging, analytics, etc.), "score": 0, "post_type": Wednesday.PostType.question, "last_activity_date": 2017-07-01 01:26:24 +0000, "owner": ["link": https://www.gravatar.com/avatar/e819b2408e9528b895dc192aee309912?s=128&d=identicon&r=PG&f=1, "user_type": Wednesday.User.UserType.registered, "user_id": 108546, "display_name": "ravun", "reputation": 743, "accept_rate": 86], "last_edit_date": 2017-07-01 01:26:24 +0000]

],

"quota_remaining": 221,

"quota_max": 300, "has_more": false]

Notice "has_more": false -- so the API thinks it has provided all the information.

Using this query ("Query 2"):

/2.2/posts/44218623;44217105;44262473;44272520;44309003;44305981;44319988;44318730;44330107;44329638;44334480;44350820;44354106;44358187;44374565;44374073;44365138;44377970;44388874;44395390;44397545;44401141;44399160;44408394;44454372;44446876;44458876;44461402;44468764;44650412

I get back 0 entries. These are all valid ids

(lldb) po result

["quota_max": 300, "quota_remaining": 224, "has_more": false]

Here is a sample of some the Swift 3.2 code used to call (I don't believe the swift code is the issue):

let response: String = try get(url)
guard let json = try parseJSON(response) as? [String:Any] else {
    throw APIError.notDictionary(response: response)
}

open func get(_ url: String) throws -> (Data, HTTPURLResponse) {
    guard let nsUrl = URL(string: url) else {
        throw RequestError.invalidURL(url: url)
    }
    var request = URLRequest(url: nsUrl)
    request.setValue(String(request.httpBody?.count ?? 0), forHTTPHeaderField: "Content-Length")
    return try performRequest(request)
}

How do I retrieve a list of 30 posts using the 2.2 API? Is there a newer API?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Mozahler
  • 4,958
  • 6
  • 36
  • 56

1 Answers1

1

The most important thing is that the API does not return deleted posts. There is a feature request for this, you might want to go upvote it.

However, deleted posts return in the API is buggy, so you might temporarily see some deleted posts, as well as deleted posts that you're not supposed to be able to.

Also note, that as a user with less than 10K, theoretically you should never be able to see deleted posts unless you were the author.

In your doc link, you have 3 ids (3743, 327738, and 339426):

This may be a serious bug.


In your "Query 1", all but the last 3 posts are deleted, so the API is returning the correct entries.

In your "Query 2", all 30 posts are deleted, so the API is correct to return nothing.


Re: "Is there a newer API?"

No. Not officially.
Unofficially, work seems to have started on version 3.0, but it is nowhere near ready for use.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Let me start with a "Thank you" and an up vote. I've been running with the assumption that post_ids were permanent, and have been saving those of interest in a Realm db for later research (things I can answer, things you might "favorite", etc.) Should I have been using question_id ?? If the 8K ids I've manually saved are deleted, is there a way to link to what I'm interested in: the original question (normally the post_id and title can get me a url - if the url isn't deleted.) Should I create a new question asking how to store pointers to interesting posts? – Mozahler Jul 06 '17 at 21:43
  • `post_id`s *are* permanent. It's just that we lesser mortals can't always see them. ;) ... `question_id` and `post_id` are the same if the post is a question. Using `post_id` is fine (bugs excluded). ... You can link any post, even answers, with a URL like `https://stackoverflow.com/q/327738`. If the post id after the `/q/` is an answer, it will still work but redirect to a URL like `https://stackoverflow.com/questions/327654/hashtable-to-dictionary-syncroot/327738#327738`. However, if the post is deleted, you will not be able to see it until you get 10K. But `SEDE` has deleted posts. – Brock Adams Jul 06 '17 at 21:53
  • Thanks again! I'm writing up a new question on "what needs to be saved" to return to a post later. – Mozahler Jul 06 '17 at 21:55
  • Just post id needs to be saved. *Nothing* gets you around the 10K barrier for deleted posts though (except SEDE IIRC). – Brock Adams Jul 06 '17 at 21:57
  • Oh. by delete I thought you were saying replaced by new versions (edits with a new post_id, etc). You mean, really really deleted. I'm not interested in deleted posts -- I'm amazed that 90% of the questions (from the past month or so) I was interested in were deleted. I guess in the future I'll try to get back to answer them more quickly. – Mozahler Jul 06 '17 at 22:04
  • Yeah, edits do not change the `post_id`. Revisions have their own id, EG https://stackoverflow.com/revisions/44952606/2, that we only occasionally have use for. – Brock Adams Jul 06 '17 at 22:08