A easy way to use requests but with decorators
Project description
decorator-http
To simplify handling HTTP requests in Python, you can utilize the decorator-http
package, which allows you to employ decorators for various HTTP methods. Begin by installing the package using the following pip command:
pip install decorator-http
Now, let's explore how to use this package with different HTTP methods:
GET
from decorator_http import get
@get('https://some-website.com', headers={'Content-Type': 'application/json'})
def get_example(data=None, response=None, error=None):
if error:
print('Failed to request details:', error)
return
print('Response data:', data)
print('Response status:', response.status_code)
get_example()
Handling Errors
You can handle errors by checking the error
parameter within the decorator function. If an error occurs, it will be passed to the function, allowing you to handle it accordingly.
@get('https://wrong-website.com', headers={'Content-Type': 'application/json'})
def get_example(data=None, response=None, error=None):
if error:
print('Failed to request details:', error)
return
print('Response data:', data)
print('Response status:', response.status_code)
get_example()
POST
from decorator_http import post, put, delete
@post('https://some-website.com', body={"hello": "world"}, headers={'Content-Type': 'application/json'})
def post_example(data=None, response=None, error=None):
if error:
print('Error:', error)
return
print('Response data:', data)
print('Response status:', response.status_code)
post_example()
Example for PUT and DELETE
For PUT and DELETE requests, you can use the respective decorators in a similar manner. Here's an example for DELETE:
@delete('https://some-website.com', headers={'Content-Type': 'application/json'})
def delete_example(data=None, response=None, error=None):
if error:
print('Error:', error)
return
print('Response data:', data)
print('Response status:', response.status_code)
delete_example()
Similarly, you can use @put
for PUT requests following a comparable structure.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.