4

How can I get the latest 30 questions with body and answers using the api?

And then, I want to get only first most voted 3 answers for each question. Is this possible with only one request?

To get the question body I use: filter=withbody.

The question body would look like this.

{
        "question_id": ...,
        "creation_date": ...,
        "last_activity_date": ...,
        "score": ...,
        "answer_count": ...,
        "accepted_answer_id": ...,
        "body": "...",
        "answers": [ ????? ]
        "title": "...",
        "tags": [
            ...
        ],
        "view_count": ...,
        "owner": {
            ...
        },
        "link": "...",
        "is_answered": true
    }
1

1 Answer 1

4

See the documentation for the /questions path and the question object spec. To get answers, merely use the /questions path with a filter that has answers enabled.

To get the latest 30 questions, sort by activity, descending, and request only the first page with a page-size of 30.

For example:

/2.1/questions?page=1&pagesize=30&order=desc&sort=activity&site=stackoverflow&filter=!-MBrU_IzpJ5H-AG6Bbzy.X-BYQe(2v-.J


Which returns results like:

{
"total": 5358467,
"page_size": 30,
"page": 1,
"type": "question",
"items": [
  {
    "question_id": 17562295,
    "creation_date": 1373429266,
    "last_activity_date": 1373435495,
    "score": 1,
    "answer_count": 4,
    "accepted_answer_id": 17562399,
    "body": "<p>I wrote a helper method,</p>\n\n<pre><code>internal static IntPtr StructToPtr(object obj)\n{\n    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));\n    Marshal.StructureToPtr(obj, ptr, false);\n    return ptr;\n}\n</code></pre>\n\n<p>Which takes a <code>struct</code> and gives me back an <code>IntPtr</code> to it. I use it as such:</p>\n\n<pre><code>public int Copy(Texture texture, Rect srcrect, Rect dstrect)\n{\n    return SDL.RenderCopy(_ptr, texture._ptr, Util.StructToPtr(srcrect), Util.StructToPtr(dstrect));\n}\n</code></pre>\n\n<p>The problem is that I only need that <code>IntPtr</code> for a split second so that I can pass it off to the C DLL,</p>\n\n<pre><code>[DllImport(\"SDL2.dll\", CallingConvention = CallingConvention.Cdecl, EntryPoint = \"SDL_RenderCopy\")]\ninternal static extern int RenderCopy(IntPtr renderer, IntPtr texture, IntPtr srcrect, IntPtr dstrect);\n</code></pre>\n\n<p>I don't really want to have to worry about freeing it; otherwise my 1-line function grows to 6:</p>\n\n<pre><code>public int Copy(Texture texture, Rect? srcrect=null, Rect? dstrect=null)\n{\n    var srcptr = Util.StructToPtr(srcrect);\n    var dstptr = Util.StructToPtr(dstrect);\n    var result = SDL.RenderCopy(_ptr, texture._ptr, srcptr, dstptr);\n    Marshal.FreeHGlobal(srcptr);\n    Marshal.FreeHGlobal(dstptr);\n    return result;\n}\n</code></pre>\n\n<p>Is there a better way to do this? Will C# eventually clean up any memory it has allocated?</p>\n\n<p>If not, is there a way I can wrap the call to <code>SDL.RenderCopy</code> in some <code>using</code> statements instead so that I don't have to do all this temporary variable + explicit freeing non-sense?</p>\n",
    "title": "If I allocate some memory with AllocHGlobal, do I have to free it with FreeHGlobal?",
    "tags": [
      "c#",
      "interop"
    ],
    "favorite_count": 1,
    "view_count": 19,
    "owner": {
      "user_id": 65387,
      "display_name": "Mark",
      "reputation": 34122,
      "user_type": "registered",
      "profile_image": "https://www.gravatar.com/avatar/b15dd469447f99c489fe08886025ddf7?d=identicon&r=PG",
      "link": "http://stackoverflow.com/users/65387/mark",
      "accept_rate": 68
    },
    "answers": [
      {
        "question_id": 17562295,
        "answer_id": 17562341,
        "creation_date": 1373429701,
        "last_activity_date": 1373429701,
        "score": 1,
        "is_accepted": false,
        "body": "<p>Yes, you have to Free it,  and the way you have got it as your 6 line program is pretty efficient.  Its the tradeoff you make when stepping outside the garbage collector. </p>\n",
        "owner": {
          "user_id": 10431,
          "display_name": "Keith Nicholas",
          "reputation": 17852,
          "user_type": "registered",
          "profile_image": "https://www.gravatar.com/avatar/22aac6106c44ae4078bd4c0a564714e0?d=identicon&r=PG",
          "link": "http://stackoverflow.com/users/10431/keith-nicholas",
          "accept_rate": 60
        },
        "title": "If I allocate some memory with AllocHGlobal, do I have to free it with FreeHGlobal?",
        "link": "http://stackoverflow.com/questions/17562295/if-i-allocate-some-memory-with-allochglobal-do-i-have-to-free-it-with-freehglob/17562341#17562341"
      },
      ...
1
  • 1
    nice, how did you create that filter? it is just a random hash, so it must be generated somehow
    – Omar
    Commented Apr 26, 2023 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .