HTTP Module Overview
The HTTP module provides comprehensive HTTP client functionality by wrapping Go's powerful net/http package. This gives Harneet programs access to full-featured HTTP capabilities without reimplementing the complex networking code.
Quick Start
Module Import
| Import HTTP Module | |
|---|---|
Architecture
The HTTP module follows the facade pattern, exposing all the essential features of Go's net/http while maintaining Harneet's simple and consistent API design. All functions return (result, error) tuples following Harneet's error handling conventions.
Core Features
✅ HTTP Client Methods
- GET requests -
http.Get(url) - POST requests -
http.Post(url, contentType, body) - PUT requests -
http.Put(url, contentType, body) - DELETE requests -
http.Delete(url) - HEAD requests -
http.Head(url)
✅ Advanced Request Building
- Custom requests -
http.NewRequest(method, url, body) - Header management -
http.SetHeader(request, key, value) - Request execution -
http.Do(request)
✅ Response Handling
- Status codes -
response["status"] - Headers -
response["headers"] - Body content -
response["body"] - Status text -
response["statusText"]
✅ Built-in Features
- Connection pooling - Automatic via Go's http.Client
- Timeout handling - 30-second default timeout
- Keep-alive connections - Managed automatically
- Compression support - Automatic gzip handling
- Error handling - Comprehensive error reporting
Response Object Structure
All HTTP functions return a response object (map) with this structure:
| Response Structure | |
|---|---|
Error Handling Pattern
The HTTP module follows Harneet's standard error handling pattern:
Integration with Other Modules
The HTTP module works seamlessly with other Harneet standard library modules:
- json - For encoding/decoding JSON request and response bodies
- cast - For type conversions of response data
- fmt - For formatting and displaying HTTP responses
- log - For logging HTTP requests and responses
- errors - For comprehensive error handling
Common Use Cases
API Client Development
Authentication & Headers
| Auth Headers | |
|---|---|
JSON API Integration
Performance Characteristics
- Connection Reuse - Automatic HTTP keep-alive connections
- Connection Pooling - Built-in connection pool management
- Timeout Control - 30-second default with customization options
- Memory Efficient - Streaming response handling
- Thread Safe - Safe for concurrent use
Security Features
- TLS/HTTPS Support - Automatic HTTPS handling
- Certificate Validation - Built-in certificate verification
- Header Security - Full control over request headers
- Authentication Support - Bearer tokens, Basic auth, custom schemes
Next Steps
- HTTP Client - Learn about basic HTTP methods (GET, POST, PUT, DELETE, HEAD)
- Request Building - Advanced request customization and header management
- Response Handling - Processing responses, status codes, and error handling
- Examples & Patterns - Real-world examples and common patterns
Future Enhancements
The HTTP module is designed to be easily extensible. Future enhancements may include:
- HTTP Server - Server-side HTTP handling
- WebSocket Support - WebSocket protocol upgrade
- File Upload - Multipart form data handling
- Streaming - Request/response streaming
- Middleware - Request/response middleware chain
- Connection Hijacking - Custom protocol support
The foundation is solid and ready for these advanced features when needed!