本章介绍Go iris Web框架响应http请求的处理方式,iris支持Html、JSON, JSONP, XML, Markdown, YAML等多种数据格式响应http请求。
提示:iris主要通过
ctx
上下文对象的参数实现各种格式的数据返回。
返回html数据
app.Get("/", func(ctx iris.Context) {
ctx.HTML("Hello <strong>%s</strong>!", "World")
})
返回json数据
app.Get("/json", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "hello", "status": iris.StatusOK})
})
返回jsonp数据
app.Get("/jsonp", func(ctx iris.Context) {
ctx.JSONP(iris.Map{"hello": "jsonp"}, iris.JSONP{Callback: "callbackName"})
})
返回xml数据
app.Get("/xml", func(ctx iris.Context) {
ctx.XML(iris.Map{"message": "hello", "status": iris.StatusOK})
})
返回markdown数据
app.Get("/markdown", func(ctx iris.Context) {
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
})
返回yaml数据
app.Get("/yaml", func(ctx iris.Context) {
ctx.YAML(iris.Map{"message": "hello", "status": iris.StatusOK})
})