A Python wrapper for cURL commands to execute API requests.
Project description
ReqCurl - Python Wrapper for cURL Commands
Overview
ReqCurl is a Python library that allows you to easily parse cURL commands and convert them into a dictionary format suitable for making HTTP requests using Python’s requests library. This parser can handle HTTP methods, headers, data, and authentication parameters typically found in cURL commands, making it easier to work with APIs when transitioning from shell scripts to Python code.
Features
- HTTP Methods: Supports standard HTTP methods (
GET,POST,PUT,DELETE, etc.). - URL Parsing: Automatically extracts the URL from the cURL command.
- Header Parsing: Handles custom headers (e.g.,
Authorization,Content-Type, etc.). - Data Parsing: Parses data passed via
-dor--dataflags (supportskey=valuepairs). - Authentication: Supports parsing user credentials from the
--userflag. - Error Handling: Provides error messages for invalid cURL commands and formats.
Table of Contents
Installation
To install the reqcurl library, you can simply use pip to install it from the Python Package Index (PyPI).
Installation via pip:
pip install reqcurl
Alternatively, you can clone this repository and install it locally.
Clone the Repository:
git clone https://github.com/your-username/reqcurl.git
cd reqcurl
pip install
Usage
Once installed, you can start using the library by importing it into your Python script. The main function is parse_curl(), which takes a cURL command as input and returns a dictionary with parsed request details.
Basic Example
from reqcurl import parse_curl
curl_command = 'curl -X GET https://api.example.com/data'
result = parse_curl(curl_command)
print(result)
Output
{
"method": "GET",
"url": "https://api.example.com/data",
"headers": {},
"data": None,
"auth": None
}
Parsing Data
For POST requests with data, use the -d or --data flags. Here's an example of parsing a POST request with data:
curl_command = 'curl -X POST https://api.example.com/data -d "key=value"'
result = parse_curl(curl_command)
print(result)
Output:
{
"method": "POST",
"url": "https://api.example.com/data",
"headers": {},
"data": {"key": "value"},
"auth": None
}
Parsing Headers
You can also add custom headers using the -H flag. Here's an example with an Authorization header:
curl_command = 'curl -X GET https://api.example.com/data -H "Authorization: Bearer token"'
result = parse_curl(curl_command)
print(result)
Output
{
"method": "GET",
"url": "https://api.example.com/data",
"headers": {
"Authorization": "Bearer token"
},
"data": None,
"auth": None
}
Authentication Parsing
Authentication can be parsed using the --user flag, typically in the format username:password. Here's an example of parsing authentication credentials:
curl_command = 'curl -X GET https://api.example.com/data --user "username:password"'
result = parse_curl(curl_command)
print(result)
Output
{
"method": "GET",
"url": "https://api.example.com/data",
"headers": {},
"data": None,
"auth": ("username", "password")
}
Tests
ReqCurl comes with unit tests to ensure that it works as expected. To run the tests, simply use pytest:
pytest tests/
OR
python -m pytest tests/
This will run all the tests defined in the tests folder and show the results in your terminal.
Test Example
Here’s a simple test case for parsing a GET request:
def test_parse_curl_basic_get():
curl_command = 'curl -X GET https://api.example.com/data'
expected_result = {
"method": "GET",
"url": "https://api.example.com/data",
"headers": {},
"data": None,
"auth": None
}
result = parse_curl(curl_command)
assert result == expected_result
Edge Case Handling
The parser also handles edge cases like missing URLs, invalid formats, and incorrect headers. Ensure that all such cases are covered in your tests.
def test_parse_invalid_curl():
curl_command = 'curl -X INVALID https://api.example.com/data'
try:
result = parse_curl(curl_command)
except ValueError as e:
assert str(e) == "Invalid HTTP method: INVALID"
Supported cURL Features
| Feature | cURL Flag | Supported | Notes |
|---|---|---|---|
| HTTP Methods | -X | ✅ | Defaults to GET if not specified |
| Headers | -H | ✅ | Parses multiple headers |
| Data/Body | -d, --data | ✅ | Supports JSON or form data |
| Authentication | --user | ✅ | Basic authentication |
| Cookies | -b, --cookie | ❌ | Planned |
| File Upload | -F, --form | ❌ | Planned |
| Proxies | -x, --proxy | ❌ | Planned |
Roadmap
- Add support for cookies, file uploads, and proxies.
- Handle multipart requests.
- Extend testing coverage for edge cases.
Contributing
We welcome contributions to ReqCurl! If you have suggestions, fixes, or improvements, please fork the repository, make your changes, and submit a pull request.
Steps to Contribute
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes.
- Write tests for the changes.
- Submit a pull request for review.
License
ReqCurl is released under the MIT License. See the LICENSE file for details.
This markdown file includes all sections, from installation to usage, tests, and contributing. Each section has been structured to be clear and easy to follow for users and contributors alike.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reqcurl-0.1.1.tar.gz.
File metadata
- Download URL: reqcurl-0.1.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c129b55dd60146df835e785fe3dfa3199135e54593c305d0825480a507c49e02
|
|
| MD5 |
a16b9e7c0d64ddc54101574053824b95
|
|
| BLAKE2b-256 |
9ad37bb7066f386399656f0535fbece2f9b8d6e33b33ff63e9d4dd48cd07897b
|
File details
Details for the file reqcurl-0.1.1-py3-none-any.whl.
File metadata
- Download URL: reqcurl-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa656229ae2dc039d18f0f4f29372fc60719357efafe5ae85569e113251731df
|
|
| MD5 |
91265e56018353db95102778cb2d5b09
|
|
| BLAKE2b-256 |
e62e595ef22275ef8a5f3d00bbbc2c7ec9d106c6835528ba0bfa0d9789f11b94
|