Python httpx example
Python httpx example
I'm happy to respond in English! Here's an example of using the httpx
library in Python:
What is httpx
?
httpx
is a fast, modern alternative to the popular requests
library for making HTTP requests in Python. It was created by Alexey Kuzmin and is designed to be faster, more reliable, and easier to use than requests
.
Installing httpx
To install httpx
, you can use pip:
pip install httpx
Making a Simple Request
Here's an example of making a simple GET request using httpx
:
import httpx
url = "https://www.example.com"
response = httpx.get(url)
print(response.status_code) # prints the status code (e.g. 200)
print(response.text) # prints the response text (HTML, JSON, etc.)
In this example, we import httpx
and then use the get()
function to send a GET request to the specified URL. The response
variable contains the HTTP response object, which has properties like status_code
and text
.
Sending POST Requests
Here's an example of making a POST request using httpx
:
import httpx
url = "https://www.example.com/submit"
data = {"name": "John", "age": 30}
response = httpx.post(url, json=data)
print(response.status_code) # prints the status code (e.g. 200)
print(response.text) # prints the response text (HTML, JSON, etc.)
In this example, we send a POST request to the specified URL with some JSON data in the request body.
Handling Cookies
httpx
also supports handling cookies:
import httpx
url = "https://www.example.com/login"
data = {"username": "user", "password": "pass"}
response = httpx.post(url, json=data)
Get the cookiecookie_name = "my_cookie"
cookie_value = response.cookies[cookie_name]
Use the cookie in a subsequent requesturl = "https://www.example.com/protected"
headers = {"Cookie": f"{cookie_name}={cookie_value}"}
response = httpx.get(url, headers=headers)
print(response.status_code) # prints the status code (e.g. 200)
print(response.text) # prints the response text (HTML, JSON, etc.)
In this example, we send a POST request to log in with some credentials and get a cookie back. We then use that cookie in a subsequent GET request to access a protected resource.
Error Handling
httpx
also supports error handling:
import httpx
url = "https://www.example.com/nonexistent"
try:
response = httpx.get(url)
except httpx.RequestError as e:
print(f"Error: {e}")
print(response) # will be None if the request failed
In this example, we try to make a GET request to a non-existent URL and catch any RequestError
exceptions that may occur. If the request fails, the response
variable will be None
.
I hope this helps! Let me know if you have any questions or need further assistance.
What is the content type of Python httpx?
The content type of Python httpx
library is a modern and efficient HTTP client library that provides an intuitive API for sending HTTP requests and interacting with web services.
At its core, httpx
is designed to be a drop-in replacement for the popular requests
library, but with additional features and performance optimizations. Here are some of the key content types that httpx
supports:
Example:
import httpx
response = httpx.get('https://example.com/api/data', json={'key': 'value'})
print(response.text) # prints the response body as a string
Binary: This content type is used for transmitting binary data, such as images or audio files.
Example:
import httpx
response = httpx.get('https://example.com/api/image', params={'id': '123'})
print(response.content) # prints the response body as a bytes object
JSON: This content type is used for transmitting JSON payloads, which are commonly used in web APIs.
Example:
import httpx
import json
response = httpx.post('https://example.com/api/create', json={'name': 'John', 'age': 30})
print(response.json()) # prints the response body as a JSON object
Form: This content type is used for transmitting form-encoded data, which is commonly used in web forms.
Example:
import httpx
response = httpx.post('https://example.com/api/login', data={'username': 'john', 'password': 'pass'})
print(response.text) # prints the response body as a string
Multipart: This content type is used for transmitting multipart/form-data payloads, which are commonly used in file uploads.
Example:
import httpx
from io import BytesIO
file = BytesIO(b'Hello, World!')
response = httpx.post('https://example.com/api/upload', files={'file': ('test.txt', file)})
print(response.text) # prints the response body as a string
Bytes: This content type is used for transmitting raw byte data, which can be useful in certain scenarios.
Example:
import httpx
response = httpx.get('https://example.com/api/image', params={'id': '123'})
print(response.content) # prints the response body as a bytes object
In summary, httpx
provides a flexible and powerful way to interact with web services and APIs, supporting a range of content types including text, binary, JSON, form, multipart, and bytes.