1

Do I need any special kind of software to view the responses I get after requesting something? Like with Klout for example, I can get all the code returned as I need it, I just don't know where to put it. Is it because I'm missing a soiftware program or something?

1
  • 3
    Can you clarify what you're trying to do? What API call are you working with?
    – jmort253
    Commented Dec 22, 2012 at 4:19

1 Answer 1

4

Most of the Stack Exchange API can be accessed via simple HTTP GET requests. For example, to view the most recent questions on Stack Overflow, one would fetch the following URL:

http://api.stackexchange.com/2.1/questions?site=stackoverflow

The response is returned as JSON data which looks something like this:

{
  "items": [
  {
    "question_id": 13991507,
    "creation_date": 1356098349,
    "last_activity_date": 1356158222,
    "title": "Selenium 2: something wrong with “driver.findElement(By....);",
    "tags": [
      "java",
      "eclipse"
    ]
    ...
  }
  ...
}

Virtually every modern programming language in existence either has native classes / constructs for parsing JSON or an open-source library / module exists that provides JSON parsing capabilities.

In Python 2.x, for example, you can retrieve the JSON from the URL above and parse the JSON with the following code:

from json import loads
from urllib2 import urlopen
from zlib import decompress, MAX_WBITS

for q in loads(decompress(urlopen(
  'http://api.stackexchange.com/2.1/questions?site=stackoverflow'
  ).read(), 16 + MAX_WBITS))['items']:
    print '* %s' % q['title']

Of course, there are much easier ways to use the API - you will find many client libraries listed here that take most of the hard work out of integrating access to the API into your application.

For example, Stack.PY, a Python module I wrote, would make the example above much simpler:

from stackpy import Site

for q in Site('stackoverflow').questions:
    print '* %s' % q.title

All of the hassle of retrieving the data, decompressing the GZip'd response, and decoding the JSON is transparently handled by the module.


Edit: I must be tired - I missed the title of your question where you mentioned "JavaScript". Sorry for that oversight.

Not only does JavaScript support working with JSON data, but any valid JSON data is also a valid JavaScript value. You would likely use JSONP (JSON with Padding) to retrieve data from the API. The callback you specify as a parameter in the request will be invoked with the JSON response.

I started a JavaScript library for the API a while back that should hopefully demonstrate what I've described here.

1
  • Registered on StackApps just to upvote this answer :D! This is the first place I see mentioning you should do a GET request to api.stackexchange.com. The documentation kept talking about 'queries'. But a query is worthless unless you know where to send your queries.
    – 11684
    Commented Feb 6, 2013 at 17:57

You must log in to answer this question.

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