Feat: Add custom curl plugin with various aliases and functions

Signed-off-by: Keval <kevalrrakholiya@gmail.com>
This commit is contained in:
Keval 2025-01-29 16:23:21 +05:30
commit aad227bc13
2 changed files with 181 additions and 0 deletions

90
plugins/curl/README.md Normal file
View file

@ -0,0 +1,90 @@
# Oh My Zsh Curl Plugin
This plugin extends the functionality of `curl` with useful aliases and functions, making HTTP requests easier from the terminal.
## Installation
1. Add the plugin to your `.zshrc`:
```bash
plugins=(... curl)
```
3. Reload your shell configuration:
```bash
source ~/.zshrc
```
---
## Aliases
| Alias | Command | Description |
|--------------- |--------------------------------|-------------|
| `curlget` | `curl -L` | Perform a GET request (follows redirects). |
| `curlhead` | `curl -I` | Fetch only HTTP headers. |
| `curlpost` | `curl -X POST` | Send a POST request. |
| `curlput` | `curl -X PUT` | Send a PUT request. |
| `curldelete` | `curl -X DELETE` | Send a DELETE request. |
| `curljson` | `curl -H 'Content-Type: application/json'` | Use curl with a JSON content type. |
| `curldownload` | `curl -LO` | Download a file (keeps original filename). |
| `curlverbose` | `curl -v` | Show detailed request/response info. |
| `curlsilent` | `curl -s` | Silent mode (no progress or messages). |
| `curlheadonly` | `curl -I` | Fetch only response headers. |
| `curlbasic` | `curl -u` | Perform basic authentication. |
| `curlrange` | `curl -r` | Request a specific range of bytes. |
---
## Functions
### `curl_url <url>`
Fetches the content of the specified URL.
```bash
curl_url https://example.com
```
### `curl_test <url>`
Performs a HEAD request to check server response.
```bash
curl_test https://example.com
```
### `curl_download <url> <output_file>`
Downloads content from the given URL and saves it as the specified file.
```bash
curl_download https://example.com/file.zip myfile.zip
```
### `curl_to_file <url> <output_file>`
Fetches response and outputs it to a file.
```bash
curl_to_file https://example.com api-response.json
```
### `curl_json_pretty <url>`
Fetches and pretty-prints JSON response (requires `jq`).
```bash
curl_json_pretty https://api.example.com/data
```
### `curl_with_headers <url> <header>`
Sends a request with custom headers.
```bash
curl_with_headers https://example.com "Authorization: Bearer TOKEN"
```
### `curl_basic_auth <url> <username> <password>`
Sends a request with basic authentication.
```bash
curl_basic_auth https://example.com admin secretpassword
```
### `curl_follow_redirect <url>`
Follows HTTP redirects.
```bash
curl_follow_redirect https://short.url/link
```
---
Enjoy enhanced `curl` productivity with this Oh My Zsh plugin!

View file

@ -0,0 +1,91 @@
# curl.plugin.zsh
# Function to quickly curl a URL
curl_url() {
if [[ -z "$1" ]]; then
echo "Usage: curl_url <url>"
return 1
fi
curl -L "$1"
}
# Function for testing curl response
curl_test() {
if [[ -z "$1" ]]; then
echo "Usage: curl_test <url>"
return 1
fi
curl -I "$1"
}
# Function to download a file using curl
curl_download() {
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: curl_download <url> <output_file>"
return 1
fi
curl -o "$2" "$1"
}
# Aliases for common curl operations
alias curlget='curl -L' # Short alias for performing curl GET request
alias curlhead='curl -I' # Short alias for getting the HTTP header info (HEAD request)
alias curlpost='curl -X POST' # Alias for sending a POST request
alias curlput='curl -X PUT' # Alias for sending a PUT request
alias curldelete='curl -X DELETE' # Alias for sending a DELETE request
alias curljson="curl -H 'Content-Type: application/json'" # curl with default JSON header
alias curldownload='curl -LO' # Alias for downloading files with `-O` (remote file name)
# Additional Aliases for convenience
alias curlverbose='curl -v' # Alias for verbose output, shows request/response
alias curlsilent='curl -s' # Silent curl, hides progress bar and output
alias curlheadonly='curl -I' # Shortcut for HTTP headers only
alias curlbasic='curl -u' # Basic auth for curl
alias curlrange='curl -r' # For range requests (e.g., resume downloads)
# Custom functions
# Fetch response and output to a file
curl_to_file() {
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: curl_to_file <url> <output_file>"
return 1
fi
curl -o "$2" "$1"
echo "File saved as $2"
}
# Function to fetch and display JSON content prettily
curl_json_pretty() {
if [[ -z "$1" ]]; then
echo "Usage: curl_json_pretty <url>"
return 1
fi
curl -s "$1" | jq . # jq for pretty JSON output
}
# Send curl request with custom headers
curl_with_headers() {
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: curl_with_headers <url> <header>"
return 1
fi
curl -H "$2" "$1"
}
# Function to perform simple curl with authentication
curl_basic_auth() {
if [[ -z "$1" || -z "$2" || -z "$3" ]]; then
echo "Usage: curl_basic_auth <url> <username> <password>"
return 1
fi
curl -u "$2:$3" "$1"
}
# Function for a safe URL redirect to follow (useful for API endpoints that send a 301/302 redirect)
curl_follow_redirect() {
if [[ -z "$1" ]]; then
echo "Usage: curl_follow_redirect <url>"
return 1
fi
curl -L "$1" # Follow redirects with -L flag
}