Related methods:

  1. func (r *Response) WriteJson(content interface{}) error
  2. func (r *Response) WriteJsonExit(content interface{}) error
  3. func (r *Response) WriteJsonP(content interface{}) error
  4. func (r *Response) WriteJsonPExit(content interface{}) error
  5. func (r *Response) WriteXml(content interface{}, rootTag ...string) error
  6. func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error

Response provides native support for JSON/XML data format output, implemented through the following methods:

  1. WriteJson* methods are used to return JSON data format, with parameters of any type, such as string, map, struct, etc. The returned Content-Type is application/json.
  2. WriteXml* methods are used to return XML data format, with parameters of any type, such as string, map, struct, etc. The returned Content-Type is application/xml.

Response - JSON/XML - 图1tip

While supporting the JSON data format, it also supports the JSONP protocol.

JSON

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.Group("/", func(group *ghttp.RouterGroup) {
  9. group.ALL("/json", func(r *ghttp.Request) {
  10. r.Response.WriteJson(g.Map{
  11. "id": 1,
  12. "name": "john",
  13. })
  14. })
  15. })
  16. s.SetPort(8199)
  17. s.Run()
  18. }

After execution, we test it using the curl tool:

  1. $ curl -i http://127.0.0.1:8199/json
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json
  4. Server: GoFrame HTTP Server
  5. Date: Sun, 05 Jan 2020 02:49:31 GMT
  6. Content-Length: 22
  7. {"id":1,"name":"john"}

JSONP

Note that when using the JSONP protocol, you must provide the callback parameter via Query.

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.Group("/", func(group *ghttp.RouterGroup) {
  9. group.ALL("/jsonp", func(r *ghttp.Request) {
  10. r.Response.WriteJsonP(g.Map{
  11. "id": 1,
  12. "name": "john",
  13. })
  14. })
  15. })
  16. s.SetPort(8199)
  17. s.Run()
  18. }

After execution, we test it using the curl tool:

  1. $ curl -i "http://127.0.0.1:8199/jsonp?callback=MyCallback"
  2. HTTP/1.1 200 OK
  3. Server: GoFrame HTTP Server
  4. Date: Sun, 05 Jan 2020 02:50:42 GMT
  5. Content-Length: 34
  6. Content-Type: text/plain; charset=utf-8
  7. MyCallback({"id":1,"name":"john"})

XML

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.Group("/", func(group *ghttp.RouterGroup) {
  9. group.ALL("/xml", func(r *ghttp.Request) {
  10. r.Response.Write(`<?xml version="1.0" encoding="UTF-8"?>`)
  11. r.Response.WriteXml(g.Map{
  12. "id": 1,
  13. "name": "john",
  14. })
  15. })
  16. })
  17. s.SetPort(8199)
  18. s.Run()
  19. }

After execution, we test it using the curl tool:

  1. $ curl -i http://127.0.0.1:8199/xml
  2. HTTP/1.1 200 OK
  3. Content-Type: application/xml
  4. Server: GoFrame HTTP Server
  5. Date: Sun, 05 Jan 2020 03:00:55 GMT
  6. Content-Length: 76
  7. <?xml version="1.0" encoding="UTF-8"?><doc><id>1</id><name>john</name></doc>