uncurl - Convert curl to python-requests
uncurl is a Python library that translates `curl` command-line requests into equivalent Python `requests` library code. It's particularly useful for converting `curl` commands copied from browser developer tools into runnable Python scripts. The current version is 0.0.11, and its release cadence is slow, with the last update in March 2021.
Common errors
-
Curl command options like -u , -X etc are rejected.
cause The `uncurl.parse()` function can be less robust with certain `curl` command-line options when trying to directly generate a `requests` string, leading to incomplete or incorrect output.fixInstead of `uncurl.parse()`, use `uncurl.parse_context()` which returns a structured object containing URL, headers, method, data, cookies, and authentication. You can then use these components to build a `requests` call more reliably. Alternatively, simplify the `curl` command before passing it to `uncurl.parse()`. -
ModuleNotFoundError: No module named 'requests'
cause `uncurl` converts `curl` commands into Python code that typically uses the `requests` library. However, `requests` is not a direct runtime dependency of `uncurl` itself and is not automatically installed when you install `uncurl`.fixYou need to explicitly install the `requests` library in your environment: `pip install requests`. -
uncurl generated requests code not working as expected
cause The translation from a `curl` command to `requests` code might not always be perfect, especially for non-standard headers, complex POST data, or specific `curl` flags (like `--compressed` which `requests` handles automatically, or certificate validation with `-k`/`--insecure`).fixExamine the original `curl` command closely. Use `uncurl.parse_context()` to get detailed components and then debug the `requests` call by inspecting `context.headers`, `context.data`, `context.cookies`, `context.auth`, and the `verify` parameter. Ensure `requests` is handling compression (often automatic) and certificate validation as intended.
Warnings
- gotcha The `uncurl` library's last release was in March 2021. This suggests a low level of active maintenance, which might affect its compatibility with very recent `curl` versions or advanced, newer `curl` features and syntax.
- gotcha Complex `curl` commands, especially those with intricate authentication mechanisms (e.g., NTLM, Kerberos), multi-part form data, or highly specific header encoding, might not be fully or accurately translated by `uncurl`.
- gotcha The `uncurl` command-line tool's clipboard integration (e.g., `pbpaste | uncurl`) is primarily demonstrated and might work best for macOS. On other operating systems, you may need to manually pipe the `curl` command to `uncurl` or install the `pyperclip` library for seamless clipboard access.
Install
-
pip install uncurl
Imports
- parse
import uncurl.parse
from uncurl import parse
- parse_context
import uncurl.parse_context
from uncurl import parse_context
Quickstart
import uncurl
import requests
curl_command = "curl 'https://pypi.python.org/pypi/uncurl' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'User-Agent: Mozilla/5.0' --compressed"
# Get structured components of the curl command
context = uncurl.parse_context(curl_command)
# Construct and execute the requests call
response = requests.request(
context.method.upper(),
context.url,
headers=context.headers,
data=context.data,
cookies=context.cookies,
auth=context.auth,
verify=(not context.insecure) # handle --insecure curl flag
)
print(f"Status Code: {response.status_code}")
# print(response.text)
# Alternatively, get the requests code as a string
# requests_code_string = uncurl.parse(curl_command)
# print(requests_code_string)