介绍。XML、JSON、YAML、ProtoBuf等渲染包mainimport("github.com/gin-gonic/gin""github.com/gin-gonic/gin/testdata/protoexample""net/http")funcmain(){r:=gin.Default()//gin.Hisashortcutformap[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){//你也可以使用astructvarmsgstruct{Namestring`json:"user"`MessagestringNumberint}msg.Name="Lena"msg.Message="hey"msg.Number=123//注意msg.Name在JSON中变成了"user"//会输出:{"user":"Lena","Message":"hey","Number":123}//JSON将给定的结构序列化为JSONintotheresponsebody。它还将Content-Type设置为"application/json"。//JSON方法会将指定的结构序列化为JSON到响应体中,内容类型Content-Type设置为:"application/j儿子"c.JSON(http.StatusOK,msg)})r.GET("/someXML",func(c*gin.Context){c.XML(http.StatusOK,gin.H{"message":"嘿","status":http.StatusOK})})r.GET("/someYAML",func(c*gin.Context){c.YAML(http.StatusOK,gin.H{"message":"嘿","status":http.StatusOK})})//Protocolbuffers是一种语言中立、平台中立的可扩展机制,用于序列化结构化数据。//Protocolbuffers(简称ProtoBuf)是一个跨语言、跨平台的框架,来自Google关于序列化结构化数据的Extension机制,//参见:https://developers.google.com/protocol-buffersr.GET("/someProtoBuf",func(c*gin.Context){reps:=[]int64{int64(1),int64(2)}label:="test"//protobuf的具体定义写在testdata/protoexamplefile中。//使用特殊的protobuf结构体protoexample.Test来定义测试数据data:=&protoexample.Test{label:&label,Reps:reps,}//注意databecomesbinarydatainthesponse//将数据序列化为二进制响应数据//将outputprotoexample.Testprotobufserializeddata//ProtoBufserializesthegivenstructasProtoBufintotheresponsebody.//ProtoBuf方法将给定的结构体序列化为ProtoBuf响应体c.ProtoBuf(http.StatusOK,data)})//Listenandserveon0.0.0.0:8080r.Run(":8080")}/*模拟测试curlhttp://localhost:8080/someJSON{"message":"hey","status":200}curlhttp://localhost:8080/moreJSON{"user":"Lena","Message":"hey","Number":123}curlhttp://localhost:8080/someXMLcurlhttp://localhost:8080/someYAMLmessage:heystatus:200curlhttp://localhost:8080/someProtoBuftest*/SecureJSOn使用SecureJSON方法来保护Json不被劫持。如果响应体是一个数组,该方法默认会在响应头中添加`while(1)`前缀。循环可以防止下面的代码被恶意执行,还可以自定义安全的前缀JSON.packagemainimport("github.com/gin-gonic/gin""net/http")funcmain(){r:=gin.Default()//Youcanalsouseyourownsecurejsonprefix//也可以自定义secureJson的前缀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",////Willoutput:while(1);["lena","austin","foo"]c.SecureJSON(http.StatusOK,names)})//Listenandserveon0.0.0.0:8080r.Run(":8080")}/*模拟请求:curlhttp://localhost:8080/someJSON)]}',["lena","austin","foo"]%*/JSONP使用JSONP可以实现跨域请求数据,如果请求中有查询字符串参数callback,将返回的数据作为参数传递给回调值(前端函数名),返回给前端作为一个整体作为响应体。JSONP是服务端和客户端跨源通信的常用方法。最大的特点是简单适用,老式浏览器都支持,对服务器的修改很小。它的基本思想是:一个网页通过添加一个元素向服务器请求JSON数据,不受同源策略限制;服务器收到请求后,将数据放入一个指定名称的回调函数中传回c.JSONP(http.StatusOK,data)})//Listenandserveon0.0.0.0:8080r.Run(":8080")//模拟客户端,请求参数中有一个回调参数,取值为x(前端函数名),最终响应内容为x("foo":"bar")//curlhttp:///127.0.0.1:8080/JSONP?callback=x}AsciiJSON使用ASCII编码对非ASCII字符进行转义编码,生成纯ASCII编码的JSON包mainimport("github.com/gin-gonic/gin""net/http")funcmain(){r:=gin.Default()r.GET("/someJSON",func(c*gin.Context){data:=gin.H{"lang":"GO语言","tag":"
",}//输出结果:{"lang":"GO\u??8bed\u8a00","tag":"\u003cbr\u003e"}//AsciiJSON方法返回一个Unicode编码的纯ASCII字符串,escapec.AsciiJSON(http.StatusOK,data)})//Listenandserveon0.0.0.0:8080r.Run(":8080")}/*模拟请求:curlhttp://localhost:8080/someJSON*/没有原始JSON通常逃避,JSON会将特殊的HTML字符转换成它们的unicode编码,例如将`<`转换为`\u003c`使用PureJSON方法获取原始未转义字符串。注意:此方法至少需要Go版本Packagemainimport"github.com/gin-gonic/gin"funcmain(){r:=gin.Default()//Servesunicodeentitiesr.GET("/json",func(c*gin.Context){c.JSON)1.6以上(200,gin.H{"html":"Hello,world!",})})//服务文字字符r.GET("/purejson",func(c*gin.Context){c.PureJSON(200,gin.H{"html":"Hello,world!",})})//listenandserveon0.0.0.0:8080r.Run(":8080")}/*模拟请求获取JSON字符串,HTML标签转义curlhttp://localhost:8080/json{"html":"\u003cb\u003eHello,world!\u003c/b\u003e"}获取原始JSON字符串curlhttp://localhost:8080/purejson{"html":"Hello,world!"}*/参考文档Gin官方仓库:https://github.com/gin-gonic/gin
