type User struct { Name string `json:"name"` Email string `json:"email"` } func createUser(c *gin.Context) { var user User if err := c.BindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // User struct populated with data from JSON // ... }
func createTask(c *gin.Context) { var data map[string]interface{} if err := c.BindJSON(&data); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Data populated as a map from JSON // ... }In this example, the function `createTask` expects a JSON payload with any arbitrary fields. The BindJSON function is used to populate a `map[string]interface{}` with data from the request body. If the binding fails, an error response is returned.