What are Python requests?
The python requests library is a third-party library for making HTTP requests to a specific URL.
How can you install and import the Python requests module?
This library can be installed using the pip package management system before being imported into the current working space:
Alternatively, we can use an HTTP library known as urllib or httplib, however, the Python documentation recommends that we use the Python requests library instead.
Using the requests library we can perform simple GET and POST requests, but first, we need to import the library as shown below:
Making GET Python requests
Now to create an HTTP get request, we simply need to write just one line of code. So what we ought to do first though is have a variable where we can store the response. Now generally this is called ‘r’ or response, however, we can call it whatever we see fit. So we can say ‘r’ is equal to requests which we are calling here and we’re going to do requests.get(). This signifies that we intend to make an HTTP request to the server.
We can then pass the URL that points to the resource that we are trying to access.
In this case, we are going to use ‘httpbin.org’ which is basically a testing site.
Since the response is stored in the ‘r’ variable here, the first thing we may want to do is print ‘r’ and see what we get.
The response object returns 200, which is an HTTP status code that signifies that the resource was successfully located and a response returned. If we wanted to see the number itself then we can instead run the code below.
Now if this failed or the web page wasn’t found, we would probably get that 404 error that we normally see in browsers when the resource requested cannot be found.
We also may want to get a different response back, maybe a text in this case. To do that we need to append a dot text when printing out the response so that in this case we have ‘r.text’ instead of ‘r.status_code’. This is particularly useful in instances such as when trying to get some data from the web or when web scraping.
This is basically what your browser sees behind, this is like the source code here.
There are other types of responses that we can generate for instance the ‘content’ gives us the bits representation if the response that we are expecting is a file or an image. Another one that’s probably going to be much more common is the JSON object’s response. When working with APIs more often than not the response is in JSON format.
In the example above we can fire off a GET request that returns a JSON response by changing dot text to ‘r.json()’.
Very much like what we would get if you were sending a GET request to an API.
Along with every HTTP request, there are a set of headers and that can be quite useful to understand what’s going on or what we are trying to achieve when we are sending a request. We can also return the header with just ‘r.headers’.
Headers provide useful information in the output above. We are able to know the content type which is applications/JSON in this case, and much more. This is particularly useful when trying to work things out about what’s happening in the server.
Making POST Python requests
A POST request is used to submit data to be processed to the server. In the example above we can do a post request by simply changing the URL and adding ‘post’ at the end and changing the GET function to a POST function instead. We are also going to print out ‘r.text’ instead.
Now when we send a POST request we need to send some data along with it. Otherwise, the website doesn’t know what we are trying to do. So inside the brackets that contain the URL, we are going to put a comma and say data is equal to and we need to send this as a dictionary or rather a JSON object, which in python works like a dictionary.
So we are going to do curly brackets and specify a key, animal and then the value is the tiger. And we’re going to post this information here to this server.
So here we have it here, here’s the response that we got from the post requests. We can see that we are sending a form with data. We can also see the headers that we sent as well, alongside the user agent that lets us know that this is a python request.
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
- Best way of using Python ListAppend
- Best way of using Python ListComprehension
- Best way of using Python Map
- Best way of using Python Operators
- Best way of using Python Pandas
- Best way of using Python Requests
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.