convert-to-requests


Nameconvert-to-requests JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/obfusk/convert-to-requests
Summaryconvert curl/fetch command to python requests
upload_time2022-12-14 09:07:20
maintainer
docs_urlNone
authorFC Stegerman
requires_python>=3.8
licenseGPLv3+
keywords curl fetch requests
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <!-- SPDX-FileCopyrightText: 2022 FC Stegerman <flx@obfusk.net> -->
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->

[![GitHub Release](https://img.shields.io/github/release/obfusk/convert-to-requests.svg?logo=github)](https://github.com/obfusk/convert-to-requests/releases)
[![PyPI Version](https://img.shields.io/pypi/v/convert-to-requests.svg)](https://pypi.python.org/pypi/convert-to-requests)
[![Python Versions](https://img.shields.io/pypi/pyversions/convert-to-requests.svg)](https://pypi.python.org/pypi/convert-to-requests)
[![CI](https://github.com/obfusk/convert-to-requests/workflows/CI/badge.svg)](https://github.com/obfusk/convert-to-requests/actions?query=workflow%3ACI)
[![GPLv3+](https://img.shields.io/badge/license-GPLv3+-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)

<!--
<a href="https://repology.org/project/convert-to-requests/versions">
  <img src="https://repology.org/badge/vertical-allrepos/convert-to-requests.svg?header="
    alt="Packaging status" align="right" />
</a>

<a href="https://repology.org/project/python:convert-to-requests/versions">
  <img src="https://repology.org/badge/vertical-allrepos/python:convert-to-requests.svg?header="
    alt="Packaging status" align="right" />
</a>
-->

# 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:

```bash
$ 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:

```bash
$ 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:

```bash
$ 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

```bash
$ 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

```python
>>> 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'")
```

```python
>>> 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
[...]
```

```python
>>> 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'")
```

```python
>>> 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

```bash
$ 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.

```bash
$ 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:

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

## Dependencies

* Python >= 3.8 + requests.

### Debian/Ubuntu

```bash
$ apt install python3-requests
```

## License

[![GPLv3+](https://www.gnu.org/graphics/gplv3-127x51.png)](https://www.gnu.org/licenses/gpl-3.0.html)

<!-- vim: set tw=70 sw=2 sts=2 et fdm=marker : -->

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/obfusk/convert-to-requests",
    "name": "convert-to-requests",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "curl fetch requests",
    "author": "FC Stegerman",
    "author_email": "flx@obfusk.net",
    "download_url": "https://files.pythonhosted.org/packages/ec/60/5ffd6c4f46b267e84aaf8cea6b1b6b6b2e9b3a8cc8d98f5c43ffbb64e9b1/convert-to-requests-0.2.0.tar.gz",
    "platform": null,
    "description": "<!-- SPDX-FileCopyrightText: 2022 FC Stegerman <flx@obfusk.net> -->\n<!-- SPDX-License-Identifier: GPL-3.0-or-later -->\n\n[![GitHub Release](https://img.shields.io/github/release/obfusk/convert-to-requests.svg?logo=github)](https://github.com/obfusk/convert-to-requests/releases)\n[![PyPI Version](https://img.shields.io/pypi/v/convert-to-requests.svg)](https://pypi.python.org/pypi/convert-to-requests)\n[![Python Versions](https://img.shields.io/pypi/pyversions/convert-to-requests.svg)](https://pypi.python.org/pypi/convert-to-requests)\n[![CI](https://github.com/obfusk/convert-to-requests/workflows/CI/badge.svg)](https://github.com/obfusk/convert-to-requests/actions?query=workflow%3ACI)\n[![GPLv3+](https://img.shields.io/badge/license-GPLv3+-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)\n\n<!--\n<a href=\"https://repology.org/project/convert-to-requests/versions\">\n  <img src=\"https://repology.org/badge/vertical-allrepos/convert-to-requests.svg?header=\"\n    alt=\"Packaging status\" align=\"right\" />\n</a>\n\n<a href=\"https://repology.org/project/python:convert-to-requests/versions\">\n  <img src=\"https://repology.org/badge/vertical-allrepos/python:convert-to-requests.svg?header=\"\n    alt=\"Packaging status\" align=\"right\" />\n</a>\n-->\n\n# convert-to-requests\n\n## convert curl/fetch command to python requests\n\nParse `curl` command (from \"copy to cURL\") or (w/ `--fetch`) `fetch` code (from\n\"copy to fetch\") from stdin and either execute the request using\n`requests.request()` (`exec` subcommand) or print Python code to do so (`code`\nsubcommand).\n\n### curl\n\nGet the code:\n\n```bash\n$ convert-to-requests code --pretty <<< \"curl 'https://obfusk.ch' -H 'User-Agent: Mozilla/5.0'\"\nrequests.request('GET', 'https://obfusk.ch', headers={\n    'User-Agent': 'Mozilla/5.0'\n})\n```\n\nExecute the request:\n\n```bash\n$ convert-to-requests exec -v <<< \"curl 'https://obfusk.ch' -H 'User-Agent: Mozilla/5.0'\" | head -2\nGET https://obfusk.ch headers={'User-Agent': 'Mozilla/5.0'} data=None\n<!DOCTYPE html>\n<html lang=\"en\">\n```\n\nPOST works too:\n\n```bash\n$ convert-to-requests code <<< \"curl 'https://example.com' -H 'User-Agent: Mozilla/5.0' -H 'Accept: application/json' -X POST --data-raw foo\"\nrequests.request('POST', 'https://example.com', headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'application/json'}, data=b'foo')\n```\n\n### fetch\n\n```bash\n$ convert-to-requests --fetch code <<-END\nfetch(\"https://example.com\", {\n  \"headers\": {\n    \"accept\": \"application/json\"\n  },\n  \"body\": null,\n  \"method\": \"GET\",\n  \"mode\": \"cors\",\n  \"credentials\": \"omit\"\n});\nEND\nWarning: ignoring mode=\nWarning: ignoring credentials=\nrequests.request('GET', 'https://example.com', headers={'accept': 'application/json'})\n```\n\n## Python API\n\n```python\n>>> from convert_to_requests import curl_to_requests, to_python_code\n>>> req = curl_to_requests(r\"curl 'https://example.com' -X POST --data-raw $'\\'foo\\''\")\n>>> req\nRequestData(method='POST', url='https://example.com', headers={}, data=b\"'foo'\", ignored=[])\n>>> print(to_python_code(req.method, req.url, req.headers, req.data))\nrequests.request('POST', 'https://example.com', headers={}, data=b\"'foo'\")\n>>> print(req.code())               # shorter alternative\nrequests.request('POST', 'https://example.com', headers={}, data=b\"'foo'\")\n```\n\n```python\n>>> import requests\n>>> r = requests.request(req.method, req.url, headers=req.headers, data=req.data)\n>>> r.raise_for_status()\n>>> print(r.text, end=\"\")\n[...]\n>>> print(req.exec().text, end=\"\")  # shorter alternative\n[...]\n```\n\n```python\n>>> from convert_to_requests import fetch_to_requests, to_python_code\n>>> req = fetch_to_requests('''fetch(\"https://example.com\", {\"headers\": {}, \"method\": \"POST\", \"body\": \"'foo'\"});''')\n>>> req\nRequestData(method='POST', url='https://example.com', headers={}, data=b\"'foo'\", ignored=[])\n>>> print(to_python_code(req.method, req.url, req.headers, req.data))\nrequests.request('POST', 'https://example.com', headers={}, data=b\"'foo'\")\n```\n\n```python\n>>> from convert_to_requests import parse_dollar_string\n>>> parse_dollar_string(r\"$'\\'foo\\''\")\n(\"'foo'\", '')\n```\n\n## CAVEATS\n\n### fetch\n\nUnfortunately, \"copy as fetch\" doesn't include cookies (\"copy as Node.js fetch\"\ndoes).\n\nChromium doesn't include a `User-Agent` header in either.\n\n## Installing\n\n### Using pip\n\n```bash\n$ pip install convert-to-requests\n```\n\nNB: depending on your system you may need to use e.g. `pip3 --user`\ninstead of just `pip`.\n\n### From git\n\nNB: this installs the latest development version, not the latest\nrelease.\n\n```bash\n$ git clone https://github.com/obfusk/convert-to-requests.git\n$ cd convert-to-requests\n$ pip install -e .\n```\n\nNB: you may need to add e.g. `~/.local/bin` to your `$PATH` in order\nto run `convert-to-requests`.\n\nTo update to the latest development version:\n\n```bash\n$ cd convert-to-requests\n$ git pull --rebase\n```\n\n## Dependencies\n\n* Python >= 3.8 + requests.\n\n### Debian/Ubuntu\n\n```bash\n$ apt install python3-requests\n```\n\n## License\n\n[![GPLv3+](https://www.gnu.org/graphics/gplv3-127x51.png)](https://www.gnu.org/licenses/gpl-3.0.html)\n\n<!-- vim: set tw=70 sw=2 sts=2 et fdm=marker : -->\n",
    "bugtrack_url": null,
    "license": "GPLv3+",
    "summary": "convert curl/fetch command to python requests",
    "version": "0.2.0",
    "split_keywords": [
        "curl",
        "fetch",
        "requests"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "6c8c9469cdcdc3eb6f5a923e1f7a7a2b",
                "sha256": "56289e2650586b7d9d6ec2f42835991b7aaea6054fd7c21fdfc0e9c885ac767a"
            },
            "downloads": -1,
            "filename": "convert_to_requests-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6c8c9469cdcdc3eb6f5a923e1f7a7a2b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 20366,
            "upload_time": "2022-12-14T09:07:18",
            "upload_time_iso_8601": "2022-12-14T09:07:18.595420Z",
            "url": "https://files.pythonhosted.org/packages/82/1e/cb4963731e09319fa04eff5725614f249663da13b88c83cf314526a39d91/convert_to_requests-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "16aa30fed7ae3bd8d3a732b1fb166407",
                "sha256": "ae56592c6410dc226950e26914633601718f741d937a070b8c85a804eb00f970"
            },
            "downloads": -1,
            "filename": "convert-to-requests-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "16aa30fed7ae3bd8d3a732b1fb166407",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 21267,
            "upload_time": "2022-12-14T09:07:20",
            "upload_time_iso_8601": "2022-12-14T09:07:20.257691Z",
            "url": "https://files.pythonhosted.org/packages/ec/60/5ffd6c4f46b267e84aaf8cea6b1b6b6b2e9b3a8cc8d98f5c43ffbb64e9b1/convert-to-requests-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-14 09:07:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "obfusk",
    "github_project": "convert-to-requests",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "convert-to-requests"
}
        
Elapsed time: 0.02173s