Response Helpers and Status Codes
c.JSON, c.AbortWithError, and error responses
c.JSON
c.JSON(statusCode, obj) serialises obj to JSON and sets Content-Type to application/json. It is the most common response method.
c.JSON(http.StatusOK, gin.H{
"id": user.ID,
"name": user.Name,
})gin.H
gin.H is a shorthand for map[string]any. Use it for ad-hoc JSON objects without defining a struct.
c.JSON(200, gin.H{"message": "created", "id": id})All lessons in this course
- Gin Router and Route Groups
- Request Binding and Validation
- Response Helpers and Status Codes
- Gin Middleware: Auth and Logging