feat: add request body logging for HTTP imposters
Summary
- Added request body logging for HTTP imposters to enhance debugging capabilities
- Request body is included in imposter logs when present and non-empty
- Maintains existing log format while adding body information
Changes
- Modified
src/models/http/baseHttpServer.jsto log request body alongside method and URL - Body is only logged when present and non-empty to avoid cluttering logs
- Consolidated duplicate logging to provide single, clean log entry per request
Example
Before: [http:4000 api-mock] ::1:57888 => POST /api/endpoint
After: [http:4000 api-mock] ::1:57888 => POST /api/endpoint body: {"key":"value","data":"test"}
Testing
- Manual testing confirmed proper functionality
- Request bodies are logged correctly for POST/PUT requests
- GET requests without body continue to show only method and URL
- No duplicate log entries
This enhancement improves debugging experience by showing complete request information in imposter logs.
What would happen if the body is a 10MB jpg image? That is, uploading a 10MB jpg image.
@jarl-dk Good catch! You're absolutely right about large binary content. I should handle this edge case. Here are two approaches:
Option 1 - Size limit:
if (simplifiedRequest.body && Object.keys(simplifiedRequest.body).length > 0) {
const bodyStr = JSON.stringify(simplifiedRequest.body);
const maxSize = 1000; // 1KB limit
if (bodyStr.length > maxSize) {
logMessage += ` body: [${bodyStr.length} bytes - truncated]`;
} else {
logMessage += ` body: ${bodyStr}`;
}
}
Option 2 - Content-type detection:
if (request.headers['content-type']?.startsWith('image/') ||
request.headers['content-type']?.startsWith('video/')) {
logMessage += ` body: [binary content - ${request.headers['content-length']} bytes]`;
} else if (simplifiedRequest.body && Object.keys(simplifiedRequest.body).length > 0) {
logMessage += ` body: ${JSON.stringify(simplifiedRequest.body)}`;
}
I'm leaning towards Option 1 as it's simpler and handles all content types universally. What do you think?
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code
There is already logging levels defined here for debugging.
Just wondering if this would cause slowness when running stubs for performance testing when request body is large.