Gain practical insights into accessing APIs using Python's requests library, along with understanding HTTP status codes effectively. Learn how to handle common API response scenarios to enhance your coding efficiency and error handling skills.
Key Insights
- Utilize Python's widely-used requests library to effectively access APIs, demonstrated clearly through interactions with Alpha Vantage's API documentation.
- Understand critical HTTP status codes like "200 OK," "201 Created," and "404 Not Found," and leverage resources such as HTTP Status Dogs and Wikipedia for quick reference.
- Implement practical error-handling techniques in Python code by evaluating HTTP response status codes (e.g., printing "failed to retrieve data" when the status code differs from 200).
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Accessing the API, once we've got our URL set up, and this is not a small amount of work is of learning an API, it's learning, hey, it's interface. What can I hit up to get the data? Once you have that, the Python code to actually accessing that is very simple. We're gonna use the very common requests library.
In fact, this is the one that's so common that it's in the documentation at Alpha Vantage. We're gonna say response equals requests.get and give it a URL. You don't have to save the URL into a variable like this, but it's a little more readable this way.
Now that response requests.get, this function will hit up this URL, get a response from the server and put it into whatever variable we save it as. In this case, it's response. And that response will have some things on it.
For example, if we print it out, response has a property called status code. Let's hit that one up and see what happens. It's code was 200.
All status codes are easily looked up and it's pretty good to know your status codes. One of the best sites I like for learning status codes is HTTP status dogs. We got back 200 okay, which is this adorable dog.
He looks okay. If we had instead created some data on the database by making this request, we would have gotten back a 201 created code. And there are lots of them and HTTP status codes does a great job showing you all this information in a very cute, entertaining way.
One that people know all the time is 404 not found. You went somewhere on the internet that didn't exist and the response from the server was that's not a thing. 404 not found.
HTTP status codes is great. HTTP status dogs is great. It'll tell you if you click on one of the pictures exactly what it is.
And if you click here, you'll get the Wikipedia page about all the status codes. And you can read more of course in much more detail elsewhere. But for a quick check, HTTP status dogs is a great way to familiarize yourself quickly with the basic status codes.
It's also very cute. And you can always access HTTP status cats if that's your kind of thing. Now this status code 200, that means everything went well.
If on the other hand, let's say I have a typo here and run this again, I get this big old error here. If I hit up this URL, but let's say I misspell part of it. I now get back or rather, so I misspelled the domain name.
That's why I got that big error was like it was just no response from any server. This time I hit up the right server, but I misspelled the interface, right? This is like clicking on the wrong thing. I misunderstood the interface.
It was like execute the code block, execute code block, clicking over here instead of here, right? I've misunderstood it and I, instead of query, maybe I do search. And again, that's not a thing. So I get back a 404.
Again, not found is that status code. So a common thing to do here is to have some kind of indication from the code that there's kind of some error here. We can say if response.statusCode is not equal to 200, print failed to retrieve data.
That's fine, it's very general, but too many equal signs. All right, so if I run this, I get failed to retrieve data. A little better than just a number.
Okay, but let's fix that. Instead of search, let's make it query. And now we don't get any printing, no failed to retrieve at all.
Next we'll look at what happens when the data, when we do get a 200 okay, and what we can do to actually get our data.