func handleRequest(c *gin.Context) { type inputData struct { Name string `json:"name"` Age int `json:"age"` } var input inputData if err := c.ShouldBindJSON(&input); err != nil { // handle error return } // input.Name and input.Age now contain the parsed JSON data // do something with the data }
func handleRequest(c *gin.Context) { output := struct { Message string `json:"message"` Status string `json:"status"` }{ Message: "Hello, world!", Status: "success", } c.JSON(http.StatusOK, output) }In this example, we define a struct called "output" that represents the JSON data we want to send in the response. We then use the Context's "JSON" method to send the data, along with an HTTP status code.