This chapter introduces the way Go iris Web framework handles HTTP requests. Iris supports various data formats for responding to HTTP requests, including HTML, JSON, JSONP, XML, Markdown, and YAML.
Note: Iris primarily uses the parameters of the
ctx
context object to achieve data return in various formats.
Returning HTML data
app.Get("/", func(ctx iris.Context) {
ctx.HTML("สวัสดี <strong>%s</strong>!", "โลก")
})
Returning JSON data
app.Get("/json", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "สวัสดี", "status": iris.StatusOK})
})
Returning JSONP data
app.Get("/jsonp", func(ctx iris.Context) {
ctx.JSONP(iris.Map{"hello": "jsonp"}, iris.JSONP{Callback: "callbackName"})
})
Returning XML data
app.Get("/xml", func(ctx iris.Context) {
ctx.XML(iris.Map{"message": "สวัสดี", "status": iris.StatusOK})
})
Returning Markdown data
app.Get("/markdown", func(ctx iris.Context) {
ctx.Markdown([]byte("# สวัสดี Dynamic Markdown -- iris"))
})
Returning YAML data
app.Get("/yaml", func(ctx iris.Context) {
ctx.YAML(iris.Map{"message": "สวัสดี", "status": iris.StatusOK})
})