A lightweight command-line interface for making HTTP requests. Built with Python, this tool provides an easy way to make GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS requests with support for JSON data, customizable timeouts, automatic retries on failures, and a verbose mode for detailed logging.
- Simple command-line interface
- Case-insensitive commands (GET/get, POST/post etc.)
- Support for GET, PUT, POST, PATCH, DELETE, HEAD, and OPTIONS requests
- JSON data handling for POST requests
- Customizable timeout settings and automatic retries on failures
- Verbose mode for debugging: logs requests and responses
- Detailed response output
- Status code
- Response headers
- Response body (pretty-printed if JSON)
- Comprehensive error handling
- Built-in help system
- Python 3.7 or higher
- pip (Python package installer)
- virtualenv (recommended)
- Clone the repository:
git clone https://github.com/Tyrowin/PyFetch.git
cd PyFetch
- Create and activate a virtual environment (recommended):
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/MacOS
python3 -m venv venv
source venv/bin/activate
- Install the package in development mode:
pip install -e .
# 1. Normal GET request:
pyfetch GET https://httpbin.org/get
# 2. GET request with verbose mode to see retry logs and detailed output:
pyfetch GET https://httpbin.org/get --verbose
# 3. GET request with a custom header (e.g., Authorization token):
pyfetch GET https://httpbin.org/headers -H "Authorization: Bearer your_token_here"
# 4. POST request with JSON data and custom Content-Type header:
pyfetch POST https://httpbin.org/post -d '{"key": "value"}' -H "Content-Type: application/json"
# 5. PUT request example to update a resource:
pyfetch PUT https://httpbin.org/put -d '{"name": "New Name"}' -H "Content-Type: application/json"
# 6. PATCH request example to partially update a resource:
pyfetch PATCH https://httpbin.org/patch -d '{"email": "[email protected]"}' -H "Content-Type: application/json"
# 7. DELETE request to remove a resource:
pyfetch DELETE https://httpbin.org/delete
# 8. HEAD request to fetch only headers:
pyfetch HEAD https://httpbin.org/get
# 9. OPTIONS request to check allowed methods:
pyfetch OPTIONS https://httpbin.org/get
# 10. Show help message:
pyfetch HELP
The project includes a comprehensive test suite using Python's unittest framework.
Run all tests using either of these commands:
# Using Python's unittest discover
python -m unittest discover tests
# Using pip's installed test suite
pip install -e .
python setup.py test
The test suite covers:
- HTTP client functionality
- CLI commands and arguments
- Error handling and exceptions
- Input validation
- Response processing
Tests are organized in three main files:
tests/test_cli.py
- Command-line interface teststests/test_http_client.py
- HTTP client functionality teststests/test_exceptions.py
- Exception handling tests
To add new tests:
- Choose the appropriate test file based on functionality
- Create a new test method in the relevant test class
- Use unittest assertions to verify behavior
- Run the test suite to ensure all tests pass
usage: pyfetch GET [-h] [-t TIMEOUT] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
usage: pyfetch POST [-h] [-t TIMEOUT] [-d DATA] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
-d DATA, --data DATA R|JSON data for request body. Example: '{"key": "value"}'
usage: pyfetch PUT [-h] [-t TIMEOUT] [-d DATA] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
-d DATA, --data DATA R|JSON data for request body. Example: '{"key": "value"}'
usage: pyfetch PATCH [-h] [-t TIMEOUT] [-d DATA] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
-d DATA, --data DATA R|JSON data for request body. Example: '{"key": "value"}'
usage: pyfetch DELETE [-h] [-t TIMEOUT] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
usage: pyfetch HEAD [-h] [-t TIMEOUT] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
usage: pyfetch OPTIONS [-h] [-t TIMEOUT] url
positional arguments:
url Target URL
options:
-h, --help show this help message and exit
-t TIMEOUT, --timeout TIMEOUT
Request timeout in seconds (default: 30)
Status Code: 200
Headers:
content-type: application/json
cache-control: no-cache
...
Response Body:
{
"data": {
...
}
}
The CLI handles various types of errors:
- Connection errors
- Invalid JSON data
- HTTP response errors
- Request timeout errors
- Keyboard interrupts (Ctrl+C)
All errors are displayed with descriptive messages to help diagnose the issue.
- Fork the repository
- Create your feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -m 'Add some amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
- Command not found:
- Make sure the package is installed (
pip list | findstr PyFetch
) - Ensure your virtual environment is activated
- Make sure the package is installed (
- Import errors:
- Try reinstalling the package:
pip install -e .
- Make sure you're using the correct Python environment
- Try reinstalling the package:
- JSON errors:
- Verify your JSON data is properly formatted
- Use single quotes around the entire JSON string and double quotes inside
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.