Skip to main content

uncurl for httpx, fork from uncurl-requests

Project description

Forked from https://github.com/spulec/uncurl

Changes In This Fork

  1. Switched generated client code from requests to httpx.
  2. Updated proxy and redirect argument rendering to match httpx semantics.
  3. Removed Python 2 compatibility code and standardized on Python 3.
  4. Replaced legacy test tooling with pytest and simplified the test suite.
  5. Removed unused legacy dependencies such as six, mock, sure, nose, and coverage.
  6. Improved code generation safety by using Python-safe literals instead of manual string quoting.
  7. Unified cookie parsing so --cookie and Cookie: headers are handled consistently.
  8. Omit empty request arguments in generated output to produce cleaner httpx code.

Uncurl - Converting curl requests to httpx

In a nutshell

Uncurl is a library that allows you to convert curl requests into python code that uses httpx. Since the Chrome network inspector has a nifty "Copy as cURL", this tool is useful for recreating browser requests in python.

When you don't pass any arguments to uncurl, it will use whatever is in your clipboard as the curl command.

Example

$ uncurl "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Cookie: foo=bar;' -H 'Connection: keep-alive' --compressed"
httpx.get("https://pypi.python.org/pypi/uncurl-httpx", headers={
    "Accept-Encoding": "gzip,deflate,sdch",
    "Accept-Language": "en-US,en;q=0.8",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
}, cookies={
    "foo": "bar",
})

The underlying API:

import uncurl

parsed = uncurl.parse(
    "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch'"
)
print(parsed.headers)

如果你想直接发起请求,也可以调用:

import uncurl

response = uncurl.request(
    "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch'",
    timeout=5.0,
)
print(response.status_code)

If you want the generated httpx code string, ask explicitly:

import uncurl

print(
    uncurl.parse(
        "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch'",
        as_object=False,
    )
)

This prints:

httpx.get("https://pypi.python.org/pypi/uncurl-httpx", headers={
    "Accept-Encoding": "gzip,deflate,sdch",
})

You can also retrieve the components as python objects:

import uncurl

context = uncurl.parse_context(
    "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch'"
)
print(context.url)
print(context.headers)

parse() already returns a mutable object by default, so you can tweak and send it directly:

import uncurl

parsed = uncurl.parse(
    "curl 'https://pypi.python.org/pypi/uncurl-httpx' -H 'Accept-Encoding: gzip,deflate,sdch'"
)
parsed.url = "https://example.com/api"
parsed.headers["X-Debug"] = "1"

response = parsed.request(timeout=5.0)
print(response.status_code)

On Mac OS, you can also pipe input to uncurl:

pbpaste | uncurl

Install

$ pip install uncurl-httpx

Release to PyPI with GitHub Actions

This repository includes .github/workflows/publish.yml for automated releases. However, if GitHub Actions is disabled on your account, you can publish manually.

Manual Release to PyPI

Create a local .env file at the project root:

PYPI_TOKEN=pypi-your-token-from-pypi-org

Then run:

make publish

Or directly:

uv run python scripts/publish.py

The script will:

  1. Run tests
  2. Build distributions
  3. Check with twine
  4. Upload to PyPI

After upload completes, the new version will appear on PyPI.

Automated Release with GitHub Actions

This repository includes .github/workflows/publish.yml. Pushing a tag like v0.1.1 will:

  1. run the test suite,
  2. build the package,
  3. validate the built artifacts with twine check,
  4. publish to PyPI.

Before using it, configure a PyPI Trusted Publisher for this GitHub repository and workflow.

Create and push a release tag with:

git tag v0.1.1
git push origin v0.1.1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uncurl_httpx-0.1.3.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uncurl_httpx-0.1.3-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file uncurl_httpx-0.1.3.tar.gz.

File metadata

  • Download URL: uncurl_httpx-0.1.3.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for uncurl_httpx-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c4d9f387a06ac70cac9fc5d9ed06ccc6a8b4c4dbda20a0dda1b675af5dc5126b
MD5 9007445521f8543cc3a159b33de3e203
BLAKE2b-256 c1670197efdd38cf127f6c7304cbbbecb2160388c5a8c9bc2a9d1af76b965167

See more details on using hashes here.

File details

Details for the file uncurl_httpx-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: uncurl_httpx-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for uncurl_httpx-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b78296172b4f8276cdd0badb3f3cc2c453532828eb8c7b76cc6489441c981ce
MD5 d2116bba73df88fa2b00b8dc7a9ab520
BLAKE2b-256 c3ccabc148053e2fb2c071103248a4ea9fa0e3df741f94360951f5ae17a86962

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page