type Product struct { Name string `json:"name" binding:"required"` Price int `json:"price" binding:"required"` } func createProduct(c *gin.Context) { var product Product err := c.BindWith(&product, binding.JSON) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // create product logic here c.JSON(http.StatusOK, gin.H{"message": "Product created successfully"}) }
func getUser(c *gin.Context) { var user map[string]string err := c.BindWith(&user, binding.Form) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // get user logic here c.JSON(http.StatusOK, gin.H{"user": user}) }In this example, the getUser function accepts a GET request with form data for retrieving user information. The form data is bound to a map using Context BindWith and the binding.Form option. If the binding fails due to missing or invalid data, an error response is returned. Otherwise, the user information is retrieved and a success response is returned.