Rendering
Gin supports rendering responses in multiple formats including JSON, XML, YAML, ProtoBuf, HTML, and more. Every rendering method follows the same pattern: call a method on *gin.Context with an HTTP status code and the data to serialize. Gin handles content-type headers, serialization, and writing the response automatically.
// All rendering methods share this pattern:c.JSON(http.StatusOK, data) // application/jsonc.XML(http.StatusOK, data) // application/xmlc.YAML(http.StatusOK, data) // application/x-yamlc.TOML(http.StatusOK, data) // application/tomlc.ProtoBuf(http.StatusOK, data) // application/x-protobufYou can use the Accept header or a query parameter to serve the same data in multiple formats from a single handler:
package main
import ( "net/http"
"github.com/gin-gonic/gin")
func main() { router := gin.Default()
router.GET("/user", func(c *gin.Context) { user := gin.H{"name": "Lena", "role": "admin"}
switch c.Query("format") { case "xml": c.XML(http.StatusOK, user) case "yaml": c.YAML(http.StatusOK, user) default: c.JSON(http.StatusOK, user) } })
router.Run(":8080")}In this section
- XML/JSON/YAML/ProtoBuf rendering — Render responses in multiple formats with automatic content-type handling
- SecureJSON — Prevent JSON hijacking attacks in legacy browsers
- JSONP — Support cross-domain requests from legacy clients without CORS
- AsciiJSON — Escape non-ASCII characters for safe transport
- PureJSON — Render JSON without escaping HTML characters
- Serving static files — Serve directories of static assets
- Serving data from file — Serve individual files, attachments, and downloads
- Serving data from reader — Stream data from any
io.Readerto the response - HTML rendering — Render HTML templates with dynamic data
- Multiple templates — Use multiple template sets in a single application
- Bind single binary with template — Embed templates into your compiled binary