convert curl/fetch command to python requests
Project description
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 <<< "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'})
CAVEATS
curl
Firefox and Chromium produce e.g. --data-raw $'\'foo\''
when the POST data
contains single quotes. Unfortunately, shlex
can't parse this kind of
bash
-style string. Use --parse-bash-strings
to (attempt to) parse these
properly. Of course, manually rewriting to e.g. --data-raw \''foo'\'
always
works.
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
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
Built Distribution
Hashes for convert-to-requests-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83675443fa284a81909718dd78ff4879837e0e191bb9b20ca34cf6f0c677f289 |
|
MD5 | 14a572a0122ada5c0018e674c1949ba5 |
|
BLAKE2b-256 | e8a2c3621047741845b25f0343cfd289a0e4593848edd5e5ae47750576b4071c |
Hashes for convert_to_requests-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 215a616a6143ed2166acd938305c3307b3269b6331e44e4ee95947a774600cd9 |
|
MD5 | f15e1d54442728d8f0f9c33a5ea54b84 |
|
BLAKE2b-256 | 16d681601e034bedb545b7f10a5c240a5b673ae9a00ddc890f0e0e5b5d0c81ea |