Skip to content

Conversation

@tugkanboz
Copy link

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.

Include request body in HTTP imposter logs when body is present.
This enhances debugging capabilities by showing the complete request data sent to mocked endpoints in the logs.
Include request body in HTTP imposter logs when body is present.
This enhances debugging capabilities by showing the complete request data sent to mocked endpoints in the logs.
@jarl-dk
Copy link
Member

jarl-dk commented Sep 30, 2025

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

@tugkanboz
Copy link
Author

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

@sonarqubecloud
Copy link

@naveensiddappa
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants