Examples & Patterns
This section provides real-world examples and common patterns for using the HTTP module effectively in production applications.
Complete API Client Example
Retry Logic with Exponential Backoff
Parallel API Requests
Request/Response Logging
File Upload Simulation
API Rate Limiting Handler
Configuration-Driven API Client
| Config-Driven Client | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
Health Check and Service Discovery
Best Practices Summary
1. Error Handling
- Always check for network errors first
- Handle different HTTP status code ranges appropriately
- Use retry logic for transient failures
- Don't retry client errors (4xx)
2. Authentication
- Use Bearer tokens when possible
- Store tokens securely
- Handle token expiration gracefully
- Use appropriate authentication headers
3. Request Building
- Set appropriate Content-Type headers
- Include User-Agent for identification
- Use request IDs for tracing
- Set reasonable timeouts
4. Response Processing
- Validate content types
- Parse JSON safely with error handling
- Check response sizes for large responses
- Log responses for debugging
5. Performance
- Reuse connections when possible
- Implement proper retry logic
- Use appropriate timeouts
- Consider rate limiting
6. Monitoring
- Log request/response details
- Track success/failure rates
- Monitor response times
- Implement health checks
These patterns provide a solid foundation for building robust HTTP clients in Harneet applications.