mountebank icon indicating copy to clipboard operation
mountebank copied to clipboard

feat: add request body logging for HTTP imposters

Open tugkanboz opened this issue 3 months ago • 4 comments

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.js to 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.

tugkanboz avatar Sep 17 '25 13:09 tugkanboz

What would happen if the body is a 10MB jpg image? That is, uploading a 10MB jpg image.

jarl-dk avatar Sep 30 '25 12:09 jarl-dk

@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?

tugkanboz avatar Sep 30 '25 13:09 tugkanboz

There is already logging levels defined here for debugging. image Just wondering if this would cause slowness when running stubs for performance testing when request body is large.

naveensiddappa avatar Oct 01 '25 00:10 naveensiddappa