Spaces:
Runtime error
Runtime error
from curl_cffi import requests as curl_requests | |
import json | |
def custom_requests(url, method='GET', res_type='json', kwargs=None): | |
""" | |
Make a custom HTTP request | |
Args: | |
url (str): The URL to make the request to | |
method (str): HTTP method (GET, POST, PUT, DELETE) | |
res_type (str): Response type (json or text) | |
kwargs (dict): Additional arguments for the request (headers, body, etc.) | |
Returns: | |
dict/str: Response data based on res_type | |
""" | |
try: | |
# Parse kwargs if it's a string | |
if isinstance(kwargs, str): | |
kwargs = json.loads(kwargs) | |
elif kwargs is None: | |
kwargs = {} | |
# Make the request | |
response = curl_requests.request( | |
method=method.upper(), | |
url=url, | |
**kwargs, | |
impersonate='chrome101' | |
) | |
# Raise for bad status | |
response.raise_for_status() | |
# Return based on response type | |
if res_type.lower() == 'json': | |
return response.json() | |
else: | |
return response.text | |
except curl_requests.exceptions.RequestException as e: | |
return {"error": str(e)} | |
except json.JSONDecodeError: | |
return {"error": "Invalid JSON in response"} | |