Skip to main content

GitHub Release PyPI Version Python Versions CI GPLv3+

convert-to-requests

convert curl/fetch command to python requests

Parse curl command (from "copy to cURL") or (w/ --fetch) fetch code (from "copy to fetch") from stdin and either execute the request using requests.request() (exec subcommand) or print Python code to do so (code subcommand).

curl

Get the code:

$ convert-to-requests code --pretty <<< "curl 'https://obfusk.ch' -H 'User-Agent: Mozilla/5.0'"
requests.request('GET', 'https://obfusk.ch', headers={
    'User-Agent': 'Mozilla/5.0'
})

Execute the request:

$ convert-to-requests exec -v <<< "curl 'https://obfusk.ch' -H 'User-Agent: Mozilla/5.0'" | head -2
GET https://obfusk.ch headers={'User-Agent': 'Mozilla/5.0'} data=None
<!DOCTYPE html>
<html lang="en">

POST works too:

$ convert-to-requests code <<< "curl 'https://example.com' -H 'User-Agent: Mozilla/5.0' -H 'Accept: application/json' -X POST --data-raw foo"
requests.request('POST', 'https://example.com', headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'application/json'}, data=b'foo')

fetch

$ convert-to-requests --fetch code <<-END
fetch("https://example.com", {
  "headers": {
    "accept": "application/json"
  },
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "omit"
});
END
Warning: ignoring mode=
Warning: ignoring credentials=
requests.request('GET', 'https://example.com', headers={'accept': 'application/json'})

Python API

>>> from convert_to_requests import curl_to_requests, to_python_code
>>> req = curl_to_requests(r"curl 'https://example.com' -X POST --data-raw $'\'foo\''")
>>> req
RequestData(method='POST', url='https://example.com', headers={}, data=b"'foo'", ignored=[])
>>> print(to_python_code(req.method, req.url, req.headers, req.data))
requests.request('POST', 'https://example.com', headers={}, data=b"'foo'")
>>> print(req.code())               # shorter alternative
requests.request('POST', 'https://example.com', headers={}, data=b"'foo'")
>>> import requests
>>> r = requests.request(req.method, req.url, headers=req.headers, data=req.data)
>>> r.raise_for_status()
>>> print(r.text, end="")
[...]
>>> print(req.exec().text, end="")  # shorter alternative
[...]
>>> from convert_to_requests import fetch_to_requests, to_python_code
>>> req = fetch_to_requests('''fetch("https://example.com", {"headers": {}, "method": "POST", "body": "'foo'"});''')
>>> req
RequestData(method='POST', url='https://example.com', headers={}, data=b"'foo'", ignored=[])
>>> print(to_python_code(req.method, req.url, req.headers, req.data))
requests.request('POST', 'https://example.com', headers={}, data=b"'foo'")
>>> from convert_to_requests import parse_dollar_string
>>> parse_dollar_string(r"$'\'foo\''")
("'foo'", '')

CAVEATS

fetch

Unfortunately, "copy as fetch" doesn't include cookies ("copy as Node.js fetch" does).

Chromium doesn't include a User-Agent header in either.

Installing

Using pip

$ pip install convert-to-requests

NB: depending on your system you may need to use e.g. pip3 --user instead of just pip.

From git

NB: this installs the latest development version, not the latest release.

$ git clone https://github.com/obfusk/convert-to-requests.git
$ cd convert-to-requests
$ pip install -e .

NB: you may need to add e.g. ~/.local/bin to your $PATH in order to run convert-to-requests.

To update to the latest development version:

$ cd convert-to-requests
$ git pull --rebase

Dependencies

  • Python >= 3.8 + requests.

Debian/Ubuntu

$ apt install python3-requests

License

GPLv3+

Metadata

Release files for convert-to-requests 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for convert-to-requests 0.2.0
File Size Uploaded
convert-to-requests-0.2.0.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for convert-to-requests 0.2.0
File Interpreter ABI Platform
convert_to_requests-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 41.6 kB

Release files / convert-to-requests-0.2.0.tar.gz

Download URL convert-to-requests-0.2.0.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ae56592c6410dc226950e26914633601718f741d937a070b8c85a804eb00f970
BLAKE2b-256 checksum
How to use checksums
ec605ffd6c4f46b267e84aaf8cea6b1b6b6b2e9b3a8cc8d98f5c43ffbb64e9b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / convert_to_requests-0.2.0-py3-none-any.whl

Download URL convert_to_requests-0.2.0-py3-none-any.whl
Size 20.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56289e2650586b7d9d6ec2f42835991b7aaea6054fd7c21fdfc0e9c885ac767a
BLAKE2b-256 checksum
How to use checksums
821ecb4963731e09319fa04eff5725614f249663da13b88c83cf314526a39d91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page