Hello, I’m using the latest version of WireMock-G...
# help
p
Hello, I’m using the latest version of WireMock-GO and trying to configure a response with "WithJSONBody". However, I’m having trouble passing a JSONPath helper inside the body template. Is there a way to include JSONPath helpers in a WithJSONBody response or other method? Here’s the error I’m getting: "build stub request error: json: unsupported type: func(string, interface {}) (string, error)" I suspect this happens because WithJSONBody is marshalling the template as JSON and treats the helpers as unsupported types. What is the recommended approach to return a JSON body that contains WireMock helpers like {{jsonPath ...}}?
l
@Mathieu has just done some awesome work on wiremock-go. I wonder if they will be able to help ?
p
Hello again, I found the solution, and it's very simple. So in my "string" body, I just put a (\). example: error scenario: string json = "{ "name": "{{jsonPath request.body '$.metadata.name'}}" }" WillReturnResponse( wiremock.NewResponse(). WithStatus(int64(stubMetadata.ScenarioHttpStatus)). WithHeader("Content-Type", "application/json"). WithJSONBody(json ), error: "build stub request error: json: unsupported type: func(string, interface {}) (string, error)" The solution is the(\) : string json = "{ "name": \"{{jsonPath request.body '$.metadata.name'}}\" }"
🙌 1
m
Hey, I just saw this, I was on vacation the last two weeks. It seems that the issue was resolved, but I still wonder if we could make this a bit more intuitive to use. For example,
WithJSONBody
should take something that serializes to JSON instead of a json string. That would probably break the API though, but it might be worth it
On never mind, this is already supported. @Paulo Lory Wouldn't it better to do something like this instead?
Copy code
jsonData := map[string]interface{}{
		"name": "{{jsonPath request.body '$.metadata.name'}}",
	}
    
    WillReturnResponse(
            wiremock.NewResponse().
                WithStatus(int64(stubMetadata.ScenarioHttpStatus)).
                WithHeader("Content-Type", "application/json").
                WithJSONBody(jsonData),