您现在的位置是:亿华云 > 人工智能
Golang GinWeb框架5-XML/JSON/YAML/ProtoBuf等渲染
亿华云2025-10-03 08:34:04【人工智能】6人已围观
简介简介本文接着上文(Golang GinWeb框架5-绑定请求字符串/URI/请求头/复选框/表单类型)继续探索GinWeb框架。XML,JSON,YAML,ProtoBuf等渲染packagemain
简介
本文接着上文(Golang GinWeb框架5-绑定请求字符串/URI/请求头/复选框/表单类型)继续探索GinWeb框架。框架
XML,渲染JSON,YAML,ProtoBuf等渲染
package main import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/testdata/protoexample" "net/http" ) func main() { r := gin.Default() // gin.H is a shortcut for map[string]interface{ } // gin.H对象是一个map映射,键名为字符串类型, 键值是接口,所以可以传递所有的类型 r.GET("/someJSON", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "message": "hey", "status": http.StatusOK}) }) r.GET("/moreJSON", func(c *gin.Context) { // You also can use a struct var msg struct { Name string `json:"user"` Message string Number int } msg.Name = "Lena" msg.Message = "hey" msg.Number = 123 // Note that msg.Name becomes "user" in the JSON // Will output : { "user": "Lena", "Message": "hey", "Number": 123} //JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json". //JSON方法将给定的结构序列化为JSON到响应体, 并设置内容类型Content-Type为:"application/json" c.JSON(http.StatusOK, msg) }) r.GET("/someXML", func(c *gin.Context) { c.XML(http.StatusOK, gin.H{ "message": "hey", "status": http.StatusOK}) }) r.GET("/someYAML", func(c *gin.Context) { c.YAML(http.StatusOK, gin.H{ "message": "hey", "status": http.StatusOK}) }) //Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. //Protocol buffers(简称ProtoBuf)是来自Google的一个跨语言,跨平台,用于将结构化数据序列化的可扩展机制, //详见:https://developers.google.com/protocol-buffers r.GET("/someProtoBuf", func(c *gin.Context) { reps := []int64{ int64(1), int64(2)} label := "test" // The specific definition of protobuf is written in the testdata/protoexample file. // 使用protoexample.Test这个特别的protobuf结构来定义测试数据 data := &protoexample.Test{ Label: &label, Reps: reps, } // Note that data becomes binary data in the response //将data序列化为二进制的响应数据 // Will output protoexample.Test protobuf serialized data // ProtoBuf serializes the given struct as ProtoBuf into the response body. // ProtoBuf方法将给定的结构序列化为ProtoBuf响应体 c.ProtoBuf(http.StatusOK, data) }) // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } /* 模拟测试 curl http://localhost:8080/someJSON { "message":"hey","status":200} curl http://localhost:8080/moreJSON { "user":"Lena","Message":"hey","Number":123} curl http://localhost:8080/someXML <map><message>hey</message><status>200</status></map> curl http://localhost:8080/someYAML message: hey status: 200 curl http://localhost:8080/someProtoBuf test */安全的JSOn
使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头, 这样的死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀.
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { r := gin.Default() // You can also use your own secure json prefix // 你也可以自定义安全Json的前缀 r.SecureJsonPrefix(")]},\n") //使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头, 这样的云服务器提供商死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀. r.GET("/someJSON", func(c *gin.Context) { names := []string{ "lena", "austin", "foo"} //names := map[string]string{ // "hello": "world", //} // Will output : while(1);["lena","austin","foo"] c.SecureJSON(http.StatusOK, names) }) // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } /* 模拟请求:curl http://localhost:8080/someJSON )]}, ["lena","austin","foo"]% */JSONP
使用JSONP可以实现跨域请求数据, 如果请求中有查询字符串参数callback, 则将返回数据作为参数传递给callback值(前端函数名),整体作为一个响应体,返回给前端.
JSONP是服务器与客户端跨源通信的常用方法. 最大特点就是简单适用, 老式浏览器全部支持, 服务器改造非常小, 它的基本思想是: 网页通过添加一个<script>元素, 向服务器请求JSON数据, 这种做法不受同源政策限制, 服务器收到请求后, 将数据放在一个指定名字的回调函数里传回来, 这样, 前端可以完成一次前端函数的调用, 而参数是后端返回的数据.
注意: 这种方式存在被劫持的风险
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { r := gin.Default() r.GET("/JSONP", func(c *gin.Context) { data := gin.H{ "foo": "bar", } //callback is x // Will output : x({ \"foo\":\"bar\"}) // 使用JSONP可以实现跨域请求数据, 如果请求中有查询字符串参数callback, 则将返回数据作为参数传递给callback值(前端函数名),整体作为一个响应体,返回给前端 //JSONP是服务器与客户端跨源通信的常用方法。最大特点就是框架简单适用,老式浏览器全部支持,云南idc服务商渲染服务器改造非常小。框架 //它的渲染基本思想是,网页通过添加一个<script>元素,框架向服务器请求JSON数据,渲染这种做法不受同源政策限制;服务器收到请求后,框架将数据放在一个指定名字的渲染回调函数里传回来 c.JSONP(http.StatusOK, data) }) // Listen and serve on 0.0.0.0:8080 r.Run(":8080") // 模拟客户端,请求参数中有callback参数,值为x(前端函数名),最后响应内容为x("foo":"bar") // curl http://127.0.0.1:8080/JSONP?callback=x }AsciiJSON
使用ASCII编码, 将非ASCII的字符进行转义和编码, 生成纯ASCII编码的JSON
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { r := gin.Default() r.GET("/someJSON", func(c *gin.Context) { data := gin.H{ "lang": "GO语言", "tag": "<br>", } // 输出结果 : { "lang":"GO\u8bed\u8a00","tag":"\u003cbr\u003e"} // AsciiJSON方法返回带有Unicode编码和转义组成的纯ASCII字符串 c.AsciiJSON(http.StatusOK, data) }) // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } /* 模拟请求:curl http://localhost:8080/someJSON */不带转义的原始JSON
通常, JSON会将特殊的HTML字符转化为他们的unicode编码, 如标签`<`转为`\u003c` 使用PureJSON方法可以得到原始不做转义的字符串.
注意: 该方法至少需要Go版本1.6以上
package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() // Serves unicode entities r.GET("/json", func(c *gin.Context) { c.JSON(200, gin.H{ "html": "<b>Hello, world!</b>", }) }) // Serves literal characters r.GET("/purejson", func(c *gin.Context) { c.PureJSON(200, gin.H{ "html": "<b>Hello, world!</b>", }) }) // listen and serve on 0.0.0.0:8080 r.Run(":8080") } /* 模拟请求,得到将HTML标签转义后的JSON字符串 curl http://localhost:8080/json { "html":"\u003cb\u003eHello, world!\u003c/b\u003e"} 得到原始JSON字符串 curl http://localhost:8080/purejson { "html":"<b>Hello, world!</b>"} */参考文档
Gin官方仓库:https://github.com/gin-gonic/gin
很赞哦!(41646)