I'm working with the Fiber framework in Go and need to modify the request body to include URL parameters. Specifically, I want to combine both the request body and URL parameters into a single request form.
I've searched the Fiber documentation, but I couldn't find a function similar to setBody in the context (c). Also, I'd prefer not to use c.Local() for this purpose.
Has anyone encountered this situation or have a solution?
func AddParamToBody(paramName string) fiber.Handler {
return func(c *fiber.Ctx) error {
paramValue := c.Params(paramName)
var bodyMap map[string]interface{}
json.Unmarshal(c.Body(), &bodyMap);
bodyMap[paramName] = paramValue
newBody, err := json.Marshal(bodyMap)
// this is caused error, no SetBody() function exist in fiber framework
c.SetBody(newBody) // error
return c.Next()
}
}
I find the solution, you can't directly use SetBody() function, you sould call Request() function first.
c.Request().SetBody()