Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stackoverflow API response format

The Stackoverflow API is returning an unexpected response when C# to create a HTTP GET request.

If I paste http://api.stackoverflow.com/1.1/users/882993 into the browsers address bar I get the correct JSON response:

{
  "total": 1,
  "page": 1,
  "pagesize": 30,
  "users": [
    {
      "user_id": 882993,
      "user_type": "registered",
      "creation_date": 1312739131,
      "display_name": "Jack",
      "reputation": 1926,
      "email_hash": "69243d90e50d9e0b3e025517fd23d1da",
      "age": 23,
      "last_access_date": 1358087009,
      "website_url": "http://jtbrown.me.uk",
      "location": "Birmingham, United Kingdom",
      "about_me": "<p>Student.</p>\n",
      "question_count": 68,
      "answer_count": 79,
      "view_count": 115,
      "up_vote_count": 98,
      "down_vote_count": 3,
      "accept_rate": 94,
      "association_id": "d64187a3-bf66-4a4d-8e87-6ef18f0397e3",
      "user_questions_url": "/users/882993/questions",
      "user_answers_url": "/users/882993/answers",
      "user_favorites_url": "/users/882993/favorites",
      "user_tags_url": "/users/882993/tags",
      "user_badges_url": "/users/882993/badges",
      "user_timeline_url": "/users/882993/timeline",
      "user_mentioned_url": "/users/882993/mentioned",
      "user_comments_url": "/users/882993/comments",
      "user_reputation_url": "/users/882993/reputation",
      "badge_counts": {
        "gold": 0,
        "silver": 7,
        "bronze": 34
      }
    }
  ]
}

If I attempt to perform the same action in code:

    HttpWebRequest Request = WebRequest.Create("http://api.stackoverflow.com/1.1/users/882993") as HttpWebRequest;  

    using (HttpWebResponse Response = Request.GetResponse() as HttpWebResponse)  
    {  
        // Get the response stream  
        StreamReader Reader = new StreamReader(Response.GetResponseStream());

        // Console application output  
        StackWidget.Text = Reader.ReadToEnd();
    } 

I get the response:

�\b\0\0\0\0\0\0u�Ms�0���`8��Ӏ2�rl����#���J4��^\t#�p�g���j�����|�n�G/ڎ�7p����$�5\r���f�y�v�����\"F(���֛0���J�?{��������$���e�!T�-~+��@_p���j\fb�(�f�my��dt�ӄ�!AV\t����G'$\"؉i{;��X��5H9�z(�\"GQ<�]��TA9\b�Z��T��U%���;�n�-����*:ʚ��w�c��޹yU|P�m�S��M\r��?��O���@�m������\n'\b�}/�ь�.7E\a�*���uaDN@�k��N�L�zU\n�3�:DJ^S{����$K�\"�ɮ:f�.�)�P�\f�Qq\f�C�/�k*UN=�A\r�,7���.���p�9�3�jVT7��)ܬH\fYzW�4kGX�_|�AK��q�KU�GGw��^�j����D���7�\\��Ƴr,�N�yzno�F\ro�̄[�&i{afڵ��ٗ,���c\\~=l>6�\0U�F\0\0
like image 905
Jack Avatar asked Jan 13 '13 14:01

Jack


People also ask

What is the format of API response?

All responses from the API are formatted as JSON (JavaScript Object Notation) objects containing information related to the request, and any status. Every modern language should have libraries capable of quickly parsing JSON objects.

How do I get the API response in JSON format?

To return an API response in JSON format, send a parameter " format " in the request with a value of " json ".

What are the two most common response formats for an API?

The most common formats found in modern APIs are JSON (JavaScript Object Notation) and XML (Extensible Markup Language).

Which data format does REST API use?

The REST API supports the following data formats: application/json. application/json indicates JavaScript Object Notation (JSON) and is used for most of the resources. application/xml indicates eXtensible Markup Language (XML) and is used for selected resources.


1 Answers

The HTTP response you received is GZIP compressed, so you have to decompress the response stream. This can be automatically done by setting the HttpWebRequest.AutomaticDecompression property.

var request = (HttpWebRequest)WebRequest.Create("http://api.stackoverflow.com/1.1/users/882993");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    var json = reader.ReadToEnd();
}
like image 92
Paolo Moretti Avatar answered Sep 18 '22 14:09

Paolo Moretti