Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

HTTP Requests in Python Using the Requests Library

Tech 1

Installation and Import

Install the package:

pip install requests

Import into code:

import requests

Core Funcsionality

Basic Request Methods

Execute GET request:

response = requests.get("http://books.toscrape.com")
print(response.status_code)  # 200

Execute POST request:

response = requests.post("https://jsonplaceholder.typicode.com/posts")
print(response.status_code)  # 201

Response Handling

Check success status:

result = requests.get("https://www.example.org")
if result.ok:
    print("Operation succeeded")

Parse JSON response:

api_response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
data = api_response.json()
print(data["title"])

Handle binary content:

file_resp = requests.get("https://example.com/logo.png")
with open("logo.png", "wb") as output_file:
    output_file.write(file_resp.content)

Advanced Feautres

Add query parameters:

query_params = {"category": "fiction", "limit": 10}
response = requests.get("https://api.example.com/books", params=query_params)

Submit form data:

form_data = {"username": "test_user", "password": "securepass"}
response = requests.post("https://auth.example.com/login", data=form_data)

Customize headers:

custom_headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
    "Accept-Language": "en-US"
}
response = requests.get("https://example.com", headers=custom_headers)

Manage cookies:

session = requests.Session()
session.get("https://example.com/set_cookie")
authed_response = session.get("https://example.com/dashboard")

Bypass SSL verification:

import urllib3
urllib3.disable_warnings()
response = requests.get("https://self-signed.example.com", verify=False)

Set request timeout:

try:
    response = requests.get("https://slow-api.example.com", timeout=2.5)
except requests.exceptions.Timeout:
    print("Request exceeded time limit")

Access metadata:

resp = requests.get("https://www.example.com")
print(resp.headers)
print(resp.cookies)
print(resp.url)

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.