In this demo,
we'll see how we can use the Python requests library to make
requests to URL resources and handle responses.
I'm a little bored of httpbin.org,
so I'm going to make a request to another site here,
jsonplaceholder.typicode .com.
Observe that its main page very clearly tells us that this is a
fake online REST API for testing and prototyping.
It has a bunch of fake data that you can request and access,
such as posts, comments, albums, photos.
If you click through to the posts URL,
you'll see that the GET request will get you a number of posts in Latin.
Observe that the response is in the JSON format, and every post has a unique ID.
It's also possible for you to access a single post using its unique identifier.
A GET request to post/5 will give us the post with ID 5.
Let's start off on a brand new Jupyter Notebook where
we can explore the requests API.
Now the requests API is by far the easiest and most intuitive
API to work with HTTP requests and responses.
So unless you need very granular access to your HTTP requests,
I recommend that you use requests instead of any of the
other APIs that we've seen so far.
If you don't have this library installed on your machine,
you can get it with a simple pip install.
Set up the import statements for the modules that you'll use in this demo.
Let's take a look at the version of the requests library that we're working on.
It's 2.22 .0.
Making a GET request to a particular URL simply involves
invoking the requests.get function.
Specify the URL, and here is the response object.
The status code property gives you the status of the response.
This response obviously returned successfully.
The headers in your response are available in a
case-insensitive dictionary format.
Resp.headers will give you access to all of the headers returned.
The actual contents of the response are returned in the
text property of the response object.
Now for this particular URL,
the response that was returned is in the JSON format.
This is the to-do with ID 1.
So far, when we use the httpbin URL, we receive responses in the HTML format.
If you have a JSON response,
you can parse the JSON using the JSON library in Python.
The data variable holds a string json.loads,
converts this JSON string into a Python dictionary.
Observe that the data variable now contains the same
information in the form of a Python dictionary,
which means you can look up values using specific keys.
I'm going to try and make a request using this requests
library to the Wikipedia Search page.
First, let's take a look at this URL here.
I'm going to open it up on a web browser so that we can
explore this Search page on Wikipedia.
Type in requests, and let's see what the response is.
You can see a list of pages that match the search term.
I'm now going to try and make the same search request programmatically.
The wikipedia_search_url that we saw earlier accepts a
query parameter with the search term,
and this is not blocked like in the case of Google.
We'll use the requests library to make a GET
request to the wikipedia_search_url,
and the query parameter that we specify is the search for requests.
If you take a look at the response object, you'll see 200.
That means we got a successful response.
There was no error.
The search term automatically redirected us to the first search result,
and the first search result was this particular Wiki page.
Let's use the webbrowser library to open up this response
URL to see what this Wiki page looks like.
The GET request that we need to search on Wikipedia
programmatically automatically redirected us to this URL.
Let's now move on and see how we can make a POST
request using the requests library.
Here is the POST JSON data that we want to send to the server.
Now the advantage of using the requests library is that
you don't have to encode your data.
Requests.post will make a POST request to your HTTP server
and pass in your POST data directly, and we get a successful response.
And in the case of this particular site as well,
the POST response that we get in the JSON format is simply
the data that we sent in in our request.
This data is reflected back to us.