Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

fix: mock server fails to handle form encoded POST request payload#165

Open
mtronix wants to merge 1 commit intopb33f:mainfrom
mtronix:bugfix/mock-server-fails-to-handle-form-encoded-payload
Open

fix: mock server fails to handle form encoded POST request payload#165
mtronix wants to merge 1 commit intopb33f:mainfrom
mtronix:bugfix/mock-server-fails-to-handle-form-encoded-payload

Conversation

@mtronix
Copy link

@mtronix mtronix commented Feb 11, 2026

Fix for the issue:
#164

var bodyJsonObj interface{}
err = json.Unmarshal(bodyBytes, &bodyJsonObj)
if err != nil {
sms.logger.Error("Error decoding JSON of incoming request. JSON => \n%s", string(bodyBytes), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This changes the current logic of throwing an error when the json body is of wrong type. Why are we removing this check and accepting string here?

Ideally if the content-type is json then the body being string should throw a 400.

func (sms *StaticMockService) compareStructuredBody(mock StaticMockDefinitionRequest, request *http.Request) bool {
contentType := request.Header.Get("Content-Type")
// Only support JSON and form-urlencoded for structured body comparison
if contentType != "application/json" && contentType != "application/x-www-form-urlencoded" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of handling it here, we should create a separate path for handling application/x-www-form-urlencoded type content. That way the json flow stays untouched and we just add a new code path for handing this type of request.

)

// getBodyFromHttpRequest reads the body of the incoming request and returns it as an interface{}
func (sms *StaticMockService) getBodyFromHttpRequest(request *http.Request) interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Building on https://github.com/pb33f/wiretap/pull/165/changes#r2794736837

There should be a switch here and it should leave the json type flow untouched and add a new method for processing application/x-www-form-urlencoded type body.

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.

2 participants