GET method – Python requests

Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make GET request to a specified URL using requests.GET() method. Before checking out GET method, let’s figure out what a GET request is –

GET Http Method

The GET method is used to retrieve information from the given server using a given URI. The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ‘?’ character.
For example:

https://www.google.com/search?q=hello

How to make GET request through Python Requests

Python’s requests module provides in-built method called get() for making a GET request to a specified URI.

Syntax –

requests.get(url, params={key: value}, args)

Example –



Let’s try making a request to github’s APIs for example purposes.

filter_none

edit
close

play_arrow

link
brightness_4
code

import requests
   
# Making a GET request
r = requests.get('https://api.github.com / users / naveenkrnl')
  
# check status code for response received
# success code - 200
print(r)
  
# print content of request
print(r.content)

chevron_right


save this file as request.py and through terminal run,

python request.py

Output –
python-requests-get-method

Advantages of Using the GET Method

  • Since the data sent by the GET method are displayed in the URL, it is possible to bookmark the page with specific query string values.
  • GET requests can be cached and GET requests remain in the browser history.
  • GET requests can be bookmarked.

Disadvantages of Using the GET Method

  • The GET method is not suitable for passing sensitive information such as the username and password, because these are fully visible in the URL query string as well as potentially stored in the client browser’s memory as a visited page.
  • Because the GET method assigns data to a server environment variable, the length of the URL is limited. So, there is a limitation for the total data to be sent.
My Personal Notes arrow_drop_up

Software Developer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.