modify reqeust form in request body in fiber framework

89 Views Asked by At

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()
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

I find the solution, you can't directly use SetBody() function, you sould call Request() function first.

c.Request().SetBody()