Package: convert-to-requests
convert-to-requestsconvert curl/fetch command to python requestsParsecurlcommand (from "copy to cURL") or (w/--fetch)fetchcode (from
"copy to fetch") from stdin and either execute the request usingrequests.request()(execsubcommand) or print Python code to do so (codesubcommand).curlGet the code:$convert-to-requestscode--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-requestsexec-v<<<"curl 'https://obfusk.ch' -H 'User-Agent: Mozilla/5.0'"|head-2
GEThttps://obfusk.chheaders={'User-Agent':'Mozilla/5.0'}data=None
<!DOCTYPEhtml>
<htmllang="en">POST works too:$convert-to-requestscode<<<"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--fetchcode<<-ENDfetch("https://example.com", {"headers": {"accept": "application/json"},"body": null,"method": "GET","mode": "cors","credentials": "omit"});ENDWarning:ignoringmode=Warning:ignoringcredentials=requests.request('GET','https://example.com',headers={'accept':'application/json'})Python API>>>fromconvert_to_requestsimportcurl_to_requests,to_python_code>>>req=curl_to_requests(r"curl 'https://example.com' -X POST --data-raw $'\'foo\''")>>>reqRequestData(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 alternativerequests.request('POST','https://example.com',headers={},data=b"'foo'")>>>importrequests>>>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[...]>>>fromconvert_to_requestsimportfetch_to_requests,to_python_code>>>req=fetch_to_requests('''fetch("https://example.com", {"headers":{}, "method": "POST", "body": "'foo'"});''')>>>reqRequestData(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'")>>>fromconvert_to_requestsimportparse_dollar_string>>>parse_dollar_string(r"$'\'foo\''")("'foo'",'')CAVEATSfetchUnfortunately, "copy as fetch" doesn't include cookies ("copy as Node.js fetch"
does).Chromium doesn't include aUser-Agentheader in either.InstallingUsing pip$pipinstallconvert-to-requestsNB: depending on your system you may need to use e.g.pip3 --userinstead of justpip.From gitNB: this installs the latest development version, not the latest
release.$gitclonehttps://github.com/obfusk/convert-to-requests.git
$cdconvert-to-requests
$pipinstall-e.NB: you may need to add e.g.~/.local/binto your$PATHin order
to runconvert-to-requests.To update to the latest development version:$cdconvert-to-requests
$gitpull--rebaseDependenciesPython >= 3.8 + requests.Debian/Ubuntu$aptinstallpython3-requestsLicense