Search code examples
swiftstackexchange-api

Error retrieving multiple ids using the Stack Exchange API


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?


Solution

  • 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.