JSON, JSONP, XML, Markdown, YAML and MsgPack rendering

Detailed examples can be found here.

  1. func main() {
  2. app := iris.New()
  3. // iris.Map is an alias of map[string]interface{}
  4. app.Get("/json", func(ctx iris.Context) {
  5. ctx.JSON(iris.Map{"message": "hello", "status": iris.StatusOK})
  6. })
  7. // Use Secure field to prevent json hijacking.
  8. // It prepends `"while(1),"` to the body when the data is array.
  9. app.Get("/json_secure", func(ctx iris.Context) {
  10. response := []string{"val1", "val2", "val3"}
  11. options := iris.JSON{Indent: "", Secure: true}
  12. ctx.JSON(response, options)
  13. // Will output: while(1);["val1","val2","val3"]
  14. })
  15. // Use ASCII field to generate ASCII-only JSON
  16. // with escaped non-ASCII characters.
  17. app.Get("/json_ascii", func(ctx iris.Context) {
  18. response := iris.Map{"lang": "GO-虹膜", "tag": "<br>"}
  19. options := iris.JSON{Indent: " ", ASCII: true}
  20. ctx.JSON(response, options)
  21. /* Will output:
  22. {
  23. "lang": "GO-\u8679\u819c",
  24. "tag": "\u003cbr\u003e"
  25. }
  26. */
  27. })
  28. // Normally, JSON replaces special HTML characters with their unicode entities.
  29. // If you want to encode such characters literally,
  30. // you SHOULD set the UnescapeHTML field to true.
  31. app.Get("/json_raw", func(ctx iris.Context) {
  32. options := iris.JSON{UnescapeHTML: true}
  33. ctx.JSON(iris.Map{
  34. "html": "<b>Hello, world!</b>",
  35. }, options)
  36. // Will output: {"html":"<b>Hello, world!</b>"}
  37. })
  38. app.Get("/json_struct", func(ctx iris.Context) {
  39. // You also can use a struct.
  40. var msg struct {
  41. Name string `json:"user"`
  42. Message string
  43. Number int
  44. }
  45. msg.Name = "Mariah"
  46. msg.Message = "hello"
  47. msg.Number = 42
  48. // Note that msg.Name becomes "user" in the JSON.
  49. // Will output: {"user": "Mariah", "Message": "hello", "Number": 42}
  50. ctx.JSON(msg)
  51. })
  52. app.Get("/jsonp", func(ctx iris.Context) {
  53. ctx.JSONP(iris.Map{"hello": "jsonp"}, iris.JSONP{Callback: "callbackName"})
  54. })
  55. app.Get("/xml", func(ctx iris.Context) {
  56. ctx.XML(iris.Map{"message": "hello", "status": iris.StatusOK})
  57. })
  58. app.Get("/markdown", func(ctx iris.Context) {
  59. ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
  60. })
  61. app.Get("/yaml", func(ctx iris.Context) {
  62. ctx.YAML(iris.Map{"message": "hello", "status": iris.StatusOK})
  63. })
  64. app.Get("/msgpack", func(ctx iris.Context) {
  65. u := User{
  66. Firstname: "John",
  67. Lastname: "Doe",
  68. City: "Neither FBI knows!!!",
  69. Age: 25,
  70. }
  71. ctx.MsgPack(u)
  72. })
  73. // Render using jsoniter instead of the encoding/json:
  74. app.Listen(":8080", iris.WithOptimizations)
  75. }

Protobuf

Iris supports native protobuf with Protobuf and protobuf to JSON encode and decode.

  1. package main
  2. import (
  3. "app/protos"
  4. "github.com/kataras/iris/v12"
  5. )
  6. func main() {
  7. app := iris.New()
  8. app.Get("/", send)
  9. app.Get("/json", sendAsJSON)
  10. app.Post("/read", read)
  11. app.Post("/read_json", readFromJSON)
  12. app.Listen(":8080")
  13. }
  14. func send(ctx iris.Context) {
  15. response := &protos.HelloReply{Message: "Hello, World!"}
  16. ctx.Protobuf(response)
  17. }
  18. func sendAsJSON(ctx iris.Context) {
  19. response := &protos.HelloReply{Message: "Hello, World!"}
  20. options := iris.JSON{
  21. Proto: iris.ProtoMarshalOptions{
  22. AllowPartial: true,
  23. Multiline: true,
  24. Indent: " ",
  25. },
  26. }
  27. ctx.JSON(response, options)
  28. }
  29. func read(ctx iris.Context) {
  30. var request protos.HelloRequest
  31. err := ctx.ReadProtobuf(&request)
  32. if err != nil {
  33. ctx.StopWithError(iris.StatusBadRequest, err)
  34. return
  35. }
  36. ctx.Writef("HelloRequest.Name = %s", request.Name)
  37. }
  38. func readFromJSON(ctx iris.Context) {
  39. var request protos.HelloRequest
  40. err := ctx.ReadJSONProtobuf(&request)
  41. if err != nil {
  42. ctx.StopWithError(iris.StatusBadRequest, err)
  43. return
  44. }
  45. ctx.Writef("HelloRequest.Name = %s", request.Name)
  46. }