0
0
PythonHow-ToBeginner · 3 min read

How to Make GET Request in Python: Simple Guide

To make a GET request in Python, use the requests library by calling requests.get(url). This sends a request to the specified URL and returns a response object containing the server's reply.
📐

Syntax

The basic syntax to make a GET request is:

  • requests.get(url, params=None, headers=None)

Here, url is the web address you want to get data from.

params is an optional dictionary to send query parameters.

headers is an optional dictionary to send HTTP headers.

python
import requests

response = requests.get('https://example.com', params={'key': 'value'}, headers={'User-Agent': 'my-app'})
💻

Example

This example shows how to make a GET request to fetch data from a website and print the response status and content.

python
import requests

url = 'https://jsonplaceholder.typicode.com/posts/1'
response = requests.get(url)

print('Status code:', response.status_code)
print('Response body:', response.text)
Output
Status code: 200 Response body: { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
⚠️

Common Pitfalls

Common mistakes when making GET requests include:

  • Not installing the requests library before use.
  • Forgetting to check the response status code before using the data.
  • Not handling exceptions for network errors.

Always check response.status_code to ensure the request succeeded (usually 200).

python
import requests

# Wrong way: no error handling and no status check
response = requests.get('https://invalid-url')
print(response.text)

# Right way: handle exceptions and check status
try:
    response = requests.get('https://invalid-url')
    response.raise_for_status()  # Raises error for bad status
    print(response.text)
except requests.exceptions.RequestException as e:
    print('Request failed:', e)
Output
Request failed: HTTPSConnectionPool(host='invalid-url', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7f8c4c2d9d60>: Failed to resolve 'invalid-url'"))
📊

Quick Reference

Here is a quick summary of useful requests.get options:

ParameterDescription
urlThe URL to send the GET request to (string)
paramsDictionary of URL query parameters (optional)
headersDictionary of HTTP headers to send (optional)
timeoutSeconds to wait for server response (optional)
allow_redirectsFollow redirects if True (default True)

Key Takeaways

Use the requests library and call requests.get(url) to make a GET request.
Always check response.status_code to confirm the request succeeded.
Handle exceptions to catch network or connection errors gracefully.
Use the params argument to send query parameters in the URL.
Install the requests library first with pip if not already installed.