This course will become read-only in the near future. Tell us at if that is a problem.

Suit Up

Almost "Go" time... construct your query request

Back in Task 2, you were channeling your vision of a beautiful query that would return interesting information. Part of that task was to identify the Twitter API call used to issue a search. In this task we're going to construct your API call, and make sure you have the right language-specific tools to talk with the Twitter API servers. 

The search API call has a number of optional parameters, but only one required parameter. What is that parameter?

Run a test search with that single parameter in your browser. 

Feel free to add in some of the optional parameters of this API call to your query. It's up to you! If there are challenges or tricks you run into, post them to the comments. What happens if you want to use a search term with a space in it, do you need to do anything special? (Try it!)

Now we're going to set up the function call necessary to make your API request "programmatically"-- ie, in your code. This can be a bit confusing at first and there are often several ways one can do this, so for the purposes of this challenge, we are providing suggestions for which libraries to use. You can always explore other approaches (please share them below if you do!) but learning the common approach is a great way to get started. 

For both examples below, you'll need to define the api_call variable. api_call should contain the API call you constructed above (the same one you tested out in the browser). (Hint: it's just a string). 

For Python, you'll be using the urllib2 python module. Don't forget to import it, by typing

import urllib2

at the top of your script, with your other imports. To issue the API call, use urlopen function (documented here). Note that urlopen returns a "file-like object." If you're unfamiliar with file input/output in Python, you can read more about it here. As a result, to get the contents of the response, we can simply call read() on that response, as follows:

resp = urllib2.urlopen(api_call)

json_string =

For Ruby, we'll make use of the 'net/http' library. Put your require statements at the top of your script:

require 'net/http'

Now to issue the API call, we'll use the following two function calls. The first function, URI(), parses the url into a format that the following function, get(), can understand.

uri = URI(api_call)

json_string = Net::HTTP.get(uri) 

In the examples above we stored the return value of the read() or get() functions in a variable called json_string. Hmm... why might that be? Of course, we could have called it whatever we wanted. But using informative variable names helps when reading the code. People sometimes refer to this as "self-documenting code." Print our your json_string variable. Does it look like what you expected? 

What does it mean for something to be a "file-like object"?

Optional: Reading the documentation for urllib2 or net/http, can you extend you code above to check for success or error before attempting to read the HTTP response? This might be a little bit challenging, and there are lots of correct ways to do it. Feel free to ask questions and post the methods you used below. What kinds of things can happen when you don't check for errors?

Are there other places in your code that you could improve it's "self-documenting" nature? Post your improvements, if any - make sure to document what the old variable name was and the new, improved one. 

Task Discussion

  • Eenvincible said:

    Hi guys, I noticed that I did this part of the challenge before I was supposed to. Kind of jumped ahead of time or something. Either way, here is my blog post on everything I did


    on Jan. 7, 2013, 10:44 p.m.
  • govindreddy said:

    File-like objects are objects in Python that behave like a real file, e.g. have a read() and a write method(), but have a different implementation. It is and realization of the Duck Typing concept.

     File like object means just that: it's like a file, meaning it offers the same methods as file has (read, readline, ...)
    the type should be http.client.HTTPResponse

    on Dec. 6, 2012, 1:43 a.m.
  • Alvaro said:


    Here you can find the code of this Task:

    A "filel-like object" it seems to be an object with the file properties and interface.

    I had a problem with ssl in the pythonfiddle web page, so you have to test this code in your own terminal or whatever you use. :)

    on Aug. 27, 2012, 3:17 p.m.

    trbuh said:

    to me it seems as well, that a "file like object" is one that has some "meta-information" at the beginning. this is also what i found in a (fairly brief) web-search...

    on Sept. 26, 2012, 4:28 p.m. in reply to Alvaro
  • manicphase said:

    The python tutorials for JSON make it seem harder than it was. I forgot to just treat the object i got as a dictionary.


    for item in items["results"]:
        print item["text"]
    on June 1, 2012, 7:37 p.m.
  • Jessica Ledbetter said:

    For my Javascript approach, I'm using so that I can set the dataType to jsonp. 

    on April 19, 2012, 3:50 p.m.