Skip to content

Unable to initialize BodyMatcher when using Go client for Smocker API #316

Open
@suzuito

Description

@suzuito

Hi, thank you for your great work on Smocker!

I'm currently using a Go client to send requests to the Smocker API (POST /mocks) and encountered a problem: it's not possible to properly initialize the BodyMatcher struct due to the lack of constructor functions.

Here’s an example of what I'm trying to do:

package main

import (
	"bytes"
	"encoding/json"
	"net/http"

	"github.com/smocker-dev/smocker/server/types"
)

func main() {
	mocks := types.Mocks{
		{
			Request: types.MockRequest{
				Method: types.StringMatcher{Matcher: "ShouldEqual", Value: "POST"},
				Path:   types.StringMatcher{Matcher: "ShouldEqual", Value: "/api/hoge"},
				Headers: types.MultiMapMatcher{
					"Host": {{Matcher: "ShouldEqual", Value: "www.example.com"}},
				},
				Body: &types.BodyMatcher{},
			},
			Response: &types.MockResponse{
				Status: http.StatusOK,
				Headers: types.MapStringSlice{
					"Content-Type": types.StringSlice{"application/json"},
				},
				Body: `{"message":"ok"}`,
			},
		},
	}

	bodyBytes, err := json.Marshal(mocks)
	if err != nil {
		panic(err)
	}

	res, err := http.Post(http.MethodPost, "http://localhost:8081/mocks", bytes.NewReader(bodyBytes))
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

}

Activity

linked a pull request that will close this issue on Jun 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @suzuito

      Issue actions

        Unable to initialize `BodyMatcher` when using Go client for Smocker API · Issue #316 · smocker-dev/smocker