|
1 | | -# http Utility Script Documentation |
| 1 | +# 🌐 HTTP CLI Tool |
2 | 2 |
|
3 | | -## Overview |
| 3 | +A powerful command-line HTTP client that enhances `curl` with colorized output, JSON formatting, and simplified syntax for API testing and development. |
| 4 | + |
| 5 | +## 📋 Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [Features](#features) |
| 9 | +- [Installation](#installation) |
| 10 | +- [Quick Start](#quick-start) |
| 11 | +- [Usage](#usage) |
| 12 | +- [Configuration](#configuration) |
| 13 | +- [Examples](#examples) |
| 14 | +- [Requirements](#requirements) |
| 15 | + |
| 16 | +## 🎯 Overview |
4 | 17 |
|
5 | 18 | The `http` script is a command-line utility designed to simplify and enhance the usage of `curl` for making HTTP requests. It provides a user-friendly interface, colorized output, and additional features to streamline common tasks such as sending requests, viewing responses, and handling authentication. |
6 | 19 |
|
7 | | -## Key Features |
| 20 | +## ✨ Features |
8 | 21 |
|
9 | | -- **Simplified HTTP Requests:** |
| 22 | +- **🚀 Simplified HTTP Requests:** |
10 | 23 | - Supports various HTTP methods (GET, POST, PUT, DELETE, etc.) with easy syntax. |
11 | 24 | - Automatically formats and sends requests using `curl` under the hood. |
12 | 25 |
|
13 | | -- **Colorized Output:** |
| 26 | +- **🎨 Colorized Output:** |
14 | 27 | - Response headers and status codes are displayed with custom colors for better readability. |
15 | 28 | - Success, failure, and error status codes are highlighted. |
16 | 29 |
|
17 | | -- **Response Beautification:** |
| 30 | +- **💅 Response Beautification:** |
18 | 31 | - The response body is processed and beautified for easier inspection, especially for JSON content. |
| 32 | + - Automatic JSON syntax highlighting with colored keys, values, and symbols. |
19 | 33 |
|
20 | | -- **Verbose Mode:** |
| 34 | +- **🔍 Verbose Mode:** |
21 | 35 | - Optionally displays detailed response headers and additional information for debugging. |
22 | 36 |
|
23 | | -- **Authentication & Configuration:** |
| 37 | +- **🔐 Authentication & Configuration:** |
24 | 38 | - Includes commands for configuring authentication and login, making it easier to work with APIs that require credentials. |
| 39 | + - Support for headers management and persistent configuration. |
| 40 | + |
| 41 | +## 🚀 Installation |
| 42 | + |
| 43 | +### Prerequisites |
| 44 | +- `curl` must be installed and available in your system's PATH |
| 45 | +- Compatible with macOS and most Unix-like systems |
| 46 | + |
| 47 | +### Installation Steps |
| 48 | + |
| 49 | +1. **Download and make executable:** |
| 50 | + ```bash |
| 51 | + chmod +x http |
| 52 | + ``` |
| 53 | + |
| 54 | +2. **Add to PATH for global access:** |
| 55 | + |
| 56 | + **Option A: Copy to a directory already in PATH** |
| 57 | + ```bash |
| 58 | + sudo cp http /usr/local/bin/ |
| 59 | + ``` |
| 60 | + |
| 61 | + **Option B: Add ~/.local/bin to your PATH (Recommended)** |
| 62 | + ```bash |
| 63 | + # Add to your shell profile (~/.bashrc, ~/.zshrc, etc.) |
| 64 | + echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc |
| 65 | + source ~/.bashrc |
| 66 | + ``` |
| 67 | + |
| 68 | + **Option C: Create symbolic link** |
| 69 | + ```bash |
| 70 | + sudo ln -s $(pwd)/http /usr/local/bin/http |
| 71 | + ``` |
25 | 72 |
|
26 | | -## Usage |
| 73 | +3. **Verify installation:** |
| 74 | + ```bash |
| 75 | + http --help |
| 76 | + ``` |
27 | 77 |
|
28 | | -The script is invoked from the command line, accepting subcommands and options: |
| 78 | +### PATH Configuration |
29 | 79 |
|
| 80 | +To make the `http` tool available globally in your terminal, ensure `~/.local/bin` is in your PATH: |
| 81 | + |
| 82 | +```bash |
| 83 | +# For Bash users |
| 84 | +echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc |
| 85 | + |
| 86 | +# For Zsh users |
| 87 | +echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc |
| 88 | + |
| 89 | +# For Fish users |
| 90 | +fish_add_path ~/.local/bin |
30 | 91 | ``` |
31 | | -http [method] [url] [options] |
| 92 | + |
| 93 | +## ⚡ Quick Start |
| 94 | + |
| 95 | +```bash |
| 96 | +# Make a GET request |
| 97 | +http get https://api.github.com/users/octocat |
| 98 | + |
| 99 | +# POST with JSON data |
| 100 | +http post https://httpbin.org/post name=John age=30 |
| 101 | + |
| 102 | +# Add custom headers |
| 103 | +http get https://api.example.com/data Authorization:"Bearer token123" |
| 104 | + |
| 105 | +# Enable verbose mode for debugging |
| 106 | +HTTP_VERBOSE=1 http get https://api.example.com/status |
32 | 107 | ``` |
33 | 108 |
|
34 | | -### Example |
| 109 | +## 📚 Usage |
35 | 110 |
|
| 111 | +The script is invoked from the command line with flexible syntax: |
| 112 | + |
| 113 | +```bash |
| 114 | +http [method] [url] [data/options] |
36 | 115 | ``` |
37 | | -http GET https://api.example.com/resource |
| 116 | + |
| 117 | +### HTTP Methods |
| 118 | + |
| 119 | +| Method | Description | Example | |
| 120 | +|--------|-------------|---------| |
| 121 | +| `GET` | Retrieve data | `http get https://api.example.com/users` | |
| 122 | +| `POST` | Send data | `http post https://api.example.com/users name=John` | |
| 123 | +| `PUT` | Update resource | `http put https://api.example.com/users/1 name=Jane` | |
| 124 | +| `DELETE` | Delete resource | `http delete https://api.example.com/users/1` | |
| 125 | +| `PATCH` | Partial update | `http patch https://api.example.com/users/1 email=new@email.com` | |
| 126 | + |
| 127 | +### Configuration Commands |
| 128 | + |
| 129 | +| Command | Description | Example | |
| 130 | +|---------|-------------|---------| |
| 131 | +| `configure` | Set up authentication or configuration | `http configure` | |
| 132 | +| `login` | Authenticate with a service | `http login` | |
| 133 | +| `set-header` | Add persistent header | `http set-header Authorization "Bearer token"` | |
| 134 | +| `remove-header` | Remove header | `http remove-header Authorization` | |
| 135 | + |
| 136 | +## ⚙️ Configuration |
| 137 | + |
| 138 | +The tool supports persistent configuration through `.http.conf` files: |
| 139 | + |
| 140 | +### Environment Variables |
| 141 | + |
| 142 | +```bash |
| 143 | +# Enable verbose mode globally |
| 144 | +export HTTP_VERBOSE=1 |
| 145 | + |
| 146 | +# Set default timeout |
| 147 | +export HTTP_TIMEOUT=30 |
38 | 148 | ``` |
39 | 149 |
|
40 | | -### Subcommands |
41 | | -- `configure`: Set up authentication or other configuration options. |
42 | | -- `login`: Authenticate with a service or API. |
| 150 | +### Configuration File |
43 | 151 |
|
44 | | -## Response Handling |
| 152 | +Create `.http.conf` in your project directory: |
45 | 153 |
|
46 | | -- The script parses the response from `curl`, separating headers and body. |
47 | | -- Headers are colorized and shown if verbose mode is enabled. |
48 | | -- The body is cleaned of empty lines and beautified for display. |
49 | | -- The HTTP status code is extracted and shown with appropriate coloring. |
| 154 | +```bash |
| 155 | +# Base URL for requests |
| 156 | +host=https://api.example.com |
50 | 157 |
|
51 | | -## Customization |
| 158 | +# Default headers |
| 159 | +--header "Content-Type: application/json" |
| 160 | +--header "Authorization: Bearer your-token-here" |
| 161 | + |
| 162 | +# Other curl options |
| 163 | +--timeout 30 |
| 164 | +--max-time 60 |
| 165 | +``` |
52 | 166 |
|
53 | | -- Colors and formatting can be adjusted by modifying the script variables. |
54 | | -- Additional methods and options can be added as needed. |
| 167 | +## 💡 Examples |
55 | 168 |
|
56 | | -## Requirements |
| 169 | +### API Testing |
57 | 170 |
|
58 | | -- `curl` must be installed and available in your system's PATH. |
59 | | -- The script is compatible with macOS and most Unix-like systems. |
| 171 | +```bash |
| 172 | +# Test API endpoint |
| 173 | +http get https://jsonplaceholder.typicode.com/posts/1 |
| 174 | + |
| 175 | +# Create new post |
| 176 | +http post https://jsonplaceholder.typicode.com/posts \ |
| 177 | + title="My Post" body="Post content" userId=1 |
| 178 | + |
| 179 | +# Update existing post |
| 180 | +http put https://jsonplaceholder.typicode.com/posts/1 \ |
| 181 | + title="Updated Post" body="Updated content" |
| 182 | +``` |
| 183 | + |
| 184 | +### Authentication |
| 185 | + |
| 186 | +```bash |
| 187 | +# Bearer token authentication |
| 188 | +http get https://api.github.com/user \ |
| 189 | + Authorization:"Bearer ghp_your_token_here" |
| 190 | + |
| 191 | +# Basic authentication |
| 192 | +http get https://httpbin.org/basic-auth/user/pass \ |
| 193 | + --user user:pass |
| 194 | + |
| 195 | +# API key in header |
| 196 | +http get https://api.example.com/data \ |
| 197 | + X-API-Key:"your-api-key" |
| 198 | +``` |
| 199 | + |
| 200 | +### File Uploads |
| 201 | + |
| 202 | +```bash |
| 203 | +# Upload file |
| 204 | +http post https://httpbin.org/post \ |
| 205 | + --form file=@document.pdf |
| 206 | + |
| 207 | +# Upload with additional data |
| 208 | +http post https://api.example.com/upload \ |
| 209 | + --form file=@image.jpg \ |
| 210 | + --form description="Profile picture" |
| 211 | +``` |
| 212 | + |
| 213 | +### Response Handling |
| 214 | + |
| 215 | +```bash |
| 216 | +# Save response to file |
| 217 | +http get https://api.example.com/data > response.json |
| 218 | + |
| 219 | +# Show only headers |
| 220 | +http head https://api.example.com/status |
| 221 | + |
| 222 | +# Follow redirects |
| 223 | +http get https://bit.ly/short-url --location |
| 224 | +``` |
| 225 | + |
| 226 | +## 🔧 Advanced Features |
| 227 | + |
| 228 | +### Verbose Mode |
| 229 | +Enable detailed output including request headers and timing: |
| 230 | + |
| 231 | +```bash |
| 232 | +HTTP_VERBOSE=1 http get https://api.example.com/data |
| 233 | +``` |
| 234 | + |
| 235 | +### JSON Formatting |
| 236 | +Responses are automatically formatted and colorized for JSON content. |
| 237 | + |
| 238 | +### Status Code Highlighting |
| 239 | +- 🟢 Success (2xx): Green background |
| 240 | +- 🟡 Redirection (3xx): Yellow background |
| 241 | +- 🔴 Client/Server Error (4xx/5xx): Red background |
| 242 | + |
| 243 | +## 🐛 Troubleshooting |
| 244 | + |
| 245 | +### Common Issues |
| 246 | + |
| 247 | +**Command not found:** |
| 248 | +```bash |
| 249 | +# Check if http is in PATH |
| 250 | +which http |
| 251 | + |
| 252 | +# Add to PATH if needed |
| 253 | +export PATH="$HOME/.local/bin:$PATH" |
| 254 | +``` |
| 255 | + |
| 256 | +**curl not found:** |
| 257 | +```bash |
| 258 | +# Install curl (macOS) |
| 259 | +brew install curl |
| 260 | + |
| 261 | +# Install curl (Ubuntu/Debian) |
| 262 | +sudo apt-get install curl |
| 263 | +``` |
| 264 | + |
| 265 | +**Permission denied:** |
| 266 | +```bash |
| 267 | +# Make script executable |
| 268 | +chmod +x ~/.local/bin/http |
| 269 | +``` |
60 | 270 |
|
61 | | -## File Location |
| 271 | +## 📍 File Location |
62 | 272 |
|
63 | | -- The script is located at: `~/.local/bin/http` |
| 273 | +- Script location: `~/.local/bin/http` |
| 274 | +- Configuration: `.http.conf` (project-specific) or `~/.curlrc` (global) |
| 275 | +- Log files: Stored in current directory when verbose mode is enabled |
64 | 276 |
|
65 | 277 | --- |
66 | 278 |
|
67 | | -This utility is ideal for developers and power users who frequently interact with HTTP APIs and want a more convenient and visually appealing command-line experience. |
| 279 | +**💡 Pro Tip**: Use `http configure` to set up persistent authentication and `HTTP_VERBOSE=1` for debugging API issues! |
0 commit comments