Basic Usage¶
This guide covers common HTTP operations with OxideHTTP.
The examples assume an existing client created with:
HTTP methods¶
OxideHTTP provides asynchronous convenience methods for common HTTP operations.
GET¶
Retrieve a resource:
POST¶
Create a resource:
PUT¶
Replace a resource:
PATCH¶
Partially update a resource:
DELETE¶
Delete a resource:
Query parameters¶
Pass query parameters using the params argument:
Values are converted to strings when added to the request.
Boolean values are serialized as lowercase strings:
Sequence values produce repeated query parameters.
This produces a query equivalent to:
Parameters with None values are not automatically omitted by the query builder, so avoid passing them when you don't want them in the URL.
Request headers¶
Pass request-specific headers using headers:
response = await client.get(
'/users',
headers={
'Authorization': 'Bearer your-token',
'Accept': 'application/json',
},
)
Request-specific headers are combined with the client's configured default headers.
See Configuration for setting headers globally.
Request bodies¶
OxideHTTP supports several request body formats.
JSON¶
Use json for structured JSON data:
Text¶
Use body_text for plain text:
Bytes¶
Use body_bytes for raw binary data:
Form data¶
Use form to send form-encoded data:
Reading responses¶
Every request returns a response implementing BaseResponse.
Status code¶
The status_code property returns an HTTPStatus enum:
from oxidehttp.status import HTTPStatus
response = await client.get('/users')
if response.status_code == HTTPStatus.OK:
print('Request successful')
Headers¶
Response headers are available through the headers property:
Response body¶
Use the method that matches the expected response format:
# Raw bytes
data = await response.get_bytes()
# UTF-8 text
text = await response.text()
# JSON
payload = await response.json()
get_bytes() returns a memoryview. The response body is retained internally after reading, so multiple body-reading methods can be used on the same response.
Cached responses¶
When response caching is configured, a response can indicate whether it was retrieved from the cache:
See Caching for details.