| by Arround The Web | No comments

Python Requests Get Method

Python users who want to make HTTP requests should use the requests library because it is the industry standard. It hides the complexity of making requests behind an elegant and straightforward API, allowing you to focus on interacting with services and consuming the data within your application rather than dealing with those complications.

Throughout this article, you are going to learn about some of the most helpful features that requests have to offer, as well as how to personalize and optimize those capabilities for a variety of scenarios that you could find yourself in. You will also learn how to make effective use of requests, as well as how to avoid your application being sluggish as a result of requests being sent to external services.

To utilize requests, you must first load the requests library by performing the “$ pip install requests” command.

After installing it, you may use it in your Python program.

Example 1:

First, you need to import the “requests” library to use its different functions and methods. In this part, we are going to learn how to create a “GET” request. If you are using the GET method, it means that you are attempting to obtain or retrieve the data from a certain site. You must run the requests.get(URL) to create a GET request as shown in the following screenshot.  A GET request is sent to the URL provided. Now, to view the status of the request that you sent, a status code can be used.

For instance, if you receive a status of 200 OK, it indicates that your request was processed successfully. But a result of 404 NOT FOUND indicates that the resource you were seeking could not be found. There are numerous more potential status codes as well, each of which might provide you with more particular information on the status of your request.

In the following example, x.status_code returns a value of 200 which indicates that the server processed your request successfully and provided the data that you requested in its response.


 

Example 2:

In the previous example, we have seen how the status code tells us about how the server responds to our request. We can utilize that knowledge to create some programming decisions. The following snapshot illustrates one such case. Here, we added the if conditions that if the server returns a status code of 200, the program displays the message “The request was a success!”. If the response is a 404, the code displays “Result not found!”.

In this example, we will look at how we can continue a program using the response codes after hitting on HTTP links or APIs. Although the if checks in the example are used for indication of success or failure, they can be used to deide the flow of the program like verifying some data or information that are present on the website or the given link to an API or page. The 200 series response generally represents the positive case. And the 400 series response is the general representation of error from the server. Which is why we depicted the response accordingly in our following example:


 

Example 3:

A response is sent by a URI once a request is sent to it using that URI. The requests that are made provide back this Response to the requests.get() method. The response is a potent object that has a large number of functions and properties that, when combined, aid in the standardization of data and the creation of ideal sections of code.

In this example, we recieve the response of an image address online through google. Then, a file is created in “Desktop” as “f”. All the content that is received through the request.get() function is written or saved in the created file. We can see that the image icon with the name “image5.png” is created on the desktop after the following code is executed:


 

 

Example 4:  API Response

Majority of the time, requests are used to send the HTTP requests to APIs (Application Programming Interfaces).

A response is a strong entity to utilize when evaluating the outcomes of a request. Let’s make that request once more, but let’s save the result this time in a variable so that we can take a look at what is returned.

In the preceding example, we store the return value of the get() method, which is an instance of response, in a variable called “response.” The returned value is displayed using the print command. Response [200] indicates that the request was processed successfully and the server has responded.  


 

Example 5:

Authentication is the procedure of validating a user’s credentials to enable them an access to a secured resource. Authentication is the first step that must be taken to access the data, as it is impossible to grant everyone with permission to view the data from every URL. To accomplish this authentication, it is customary for an individual to supply the authentication data, utilizing an Authorization header or a custom header defined by the server.

The simplest authentication method supported by request is the “HTTP Basic Auth”. First, we need to import “HTTP Basic Auth” from requests.auth. Then, we need to execute requests.get(‘url’, auth = HTTP Basic Auth(‘user’, ‘pass’). Here, you need to provide the credentials in the form of a tuple (hostname, password). If the request is valid, it authenticates it and provides a response code 200. Otherwise, it returns an error code 403.

In the following example, we can see that the request is authenticated and a response of 200 is received, which indicates that our request was valid and is responded to by the server.


 

 

Example 6:

The Regular Expressions (re) module in Python is linked to the sub() method which is named re.sub(). It returns a string in which any occurrences of the provided pattern that, it found, will have the replacement string inserted in its place. To employ this method, we must first import the re-module.

In the following example, we use the get() function to receive the response of the web address provided and save it in the variable “a”. In the next line, all the text of the website is saved in the variable “b”. The requested response that is saved in the variable “a” is the whole HTML page saved in the link. The re.sub() function converts this text to the actual format of the HTML page from which the response was received.


 

Conclusion

The Python requests library has several built-in techniques to send the HTTP calls to a given Address using the GET method. Using the request get method, an HTTP request is used to obtain the data from a given URL. It is a request-response protocol that connects a client and a server. The client might be a web browser, while the server could be a program on a computer that hosts a website. In this article, we learned the different methods of request modules in Python with six different examples. We believe that this post helps you to grasp the various functions of Python’s requests module.

Share Button

Source: linuxhint.com

Leave a Reply