uncurl - Convert curl to python-requests

0.0.11 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `uncurl.parse_context` to extract components from a `curl` command and then build and execute a `requests` call. This method is generally more robust than `uncurl.parse` for complex commands. It also shows how to get the `requests` code as a string directly.

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)

view raw JSON →