Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

MockResponsePlugin.ReplacePlaceholders unconditionally attempts to parse request bodies as JSON, emitting a warning for non-JSON content types like application/x-www-form-urlencoded.

Changes

  • Check request.ContentType before attempting JSON deserialization
  • Parse as JSON only when Content-Type is:
    • Null/empty (backward compatibility)
    • Contains "json" (handles application/json, application/vnd.api+json, charset params, etc.)
  • For non-JSON content types, return early with debug log instead of warning
var contentType = request.ContentType;
var isJsonContent = string.IsNullOrEmpty(contentType) ||
    contentType.Contains("json", StringComparison.OrdinalIgnoreCase);

if (!isJsonContent)
{
    logger.LogDebug("Content-Type '{ContentType}' is not JSON. Skipping placeholder replacement", contentType);
    return;
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG]: MockResponsePlugin tries to parse non-JSON bodies as JSON and emits noisy warning</issue_title>
<issue_description>### Description

MockResponsePlugin attempts to parse request bodies as JSON even when the request uses the application/x-www-form-urlencoded content-type. This results in a warning:

MockResponsePlugin:  Failed to parse request body as JSON.  Placeholders in the mock response won't be replaced.

The plugin should only attempt to parse bodies as JSON when the content-type is a known JSON type or is absent (for backward compatibility).

Root Cause

The issue is in the [ReplacePlaceholders method in MockResponsePlugin.cs](https://github.com/dotnet/dev-proxy/blob/33a7bc4a7807f4a56ee847e3b12e4756dac6414a/DevProxy. Plugins/Mocking/MockResponsePlugin.cs#L528-L550). It unconditionally attempts to deserialize the request body as JSON without checking the Content-Type header first:

try
{
    var requestBody = JsonSerializer. Deserialize<JsonElement>(request.BodyString, ProxyUtils.JsonSerializerOptions);
    response.Body = ReplacePlaceholdersInObject(response. Body, requestBody, logger);
}
catch (Exception ex)
{
    logger.LogDebug(ex, "Failed to parse request body as JSON");
    logger.LogWarning("Failed to parse request body as JSON.  Placeholders in the mock response won't be replaced.");
}

Suggested Fix

Before attempting JSON parsing, check the request's Content-Type header. Only parse JSON if:

  • The content-type is a JSON type (e.g., application/json, application/vnd.api+json, etc.)
  • No content-type is set (for backwards compatibility)

Expected behaviour

The plugin should only parse the request body as JSON if the content-type indicates a JSON type or if there is no content-type header. There should be no parsing attempts (and thus no warnings) for other content-types such as application/x-www-form-urlencoded.

Actual behaviour

When the content-type is set to application/x-www-form-urlencoded, MockResponsePlugin still attempts to parse the body as JSON and emits a warning that it failed to parse. This is both noisy and misleading for users sending form-encoded requests.

Steps to reproduce

  1. Start Dev Proxy.

  2. Send a POST request with content-type set to application/x-www-form-urlencoded:

    curl -ikx http://127.0.0.1:8000 -X POST https://api.twilio.com/2010-04-01/Accounts/AC00000000000000000000000000000000/Messages. json \
        -d 'To=+15559876543' \
        -d 'From=+15551234567' \
        -d 'Body=Hello from Dev Proxy! '
  3. Observe the warning in the proxy logs:

MockResponsePlugin: Failed to parse request body as JSON.  Placeholders in the mock response won't be replaced.

Dev Proxy Version

latest from main

Operating system (environment)

macOS

Shell

zsh

Configuration file

n/a

Additional Info

The warning should only appear when the content-type is JSON or unset, not for non-JSON content-types. This change will prevent unnecessary log noise and clarify actual parsing failures.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…dicates JSON

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MockResponsePlugin to check content-type before parsing Fix MockResponsePlugin to check Content-Type before parsing body as JSON Jan 19, 2026
Copilot AI requested a review from waldekmastykarz January 19, 2026 08:46
@waldekmastykarz waldekmastykarz marked this pull request as ready for review January 19, 2026 08:54
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner January 19, 2026 08:54
Copilot AI review requested due to automatic review settings January 19, 2026 08:54
@waldekmastykarz waldekmastykarz added the pr-bugfix Fixes a bug label Jan 19, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where MockResponsePlugin.ReplacePlaceholders unconditionally attempted to parse request bodies as JSON, emitting noisy warnings for non-JSON content types like application/x-www-form-urlencoded.

Changes:

  • Added Content-Type check before attempting JSON deserialization in ReplacePlaceholders method
  • Parse as JSON only when Content-Type is null/empty (backward compatibility) or contains "json"
  • Return early with debug log (instead of warning) for non-JSON content types

@waldekmastykarz waldekmastykarz enabled auto-merge (squash) January 19, 2026 11:56
@waldekmastykarz waldekmastykarz merged commit b3213ee into main Jan 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: MockResponsePlugin tries to parse non-JSON bodies as JSON and emits noisy warning

3 participants