自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang GinWeb框架5-XML/JSON/YAML/ProtoBuf等渲染

開發(fā) 前端
本文接著上文(Golang GinWeb框架5-綁定請(qǐng)求字符串/URI/請(qǐng)求頭/復(fù)選框/表單類型)繼續(xù)探索GinWeb框架

[[355249]]

 簡(jiǎn)介

本文接著上文(Golang GinWeb框架5-綁定請(qǐng)求字符串/URI/請(qǐng)求頭/復(fù)選框/表單類型)繼續(xù)探索GinWeb框架。

XML,JSON,YAML,ProtoBuf等渲染

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "github.com/gin-gonic/gin/testdata/protoexample" 
  6.   "net/http" 
  7.  
  8. func main() { 
  9.   r := gin.Default() 
  10.  
  11.   // gin.H is a shortcut for map[string]interface{} 
  12.   // gin.H對(duì)象是一個(gè)map映射,鍵名為字符串類型, 鍵值是接口,所以可以傳遞所有的類型 
  13.   r.GET("/someJSON", func(c *gin.Context) { 
  14.     c.JSON(http.StatusOK, gin.H{"message""hey""status": http.StatusOK}) 
  15.   }) 
  16.  
  17.   r.GET("/moreJSON", func(c *gin.Context) { 
  18.     // You also can use a struct 
  19.     var msg struct { 
  20.       Name    string `json:"user"
  21.       Message string 
  22.       Number  int 
  23.     } 
  24.     msg.Name = "Lena" 
  25.     msg.Message = "hey" 
  26.     msg.Number = 123 
  27.     // Note that msg.Name becomes "user" in the JSON 
  28.     // Will output  :   {"user""Lena""Message""hey""Number": 123} 
  29.  
  30.     //JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json"
  31.     //JSON方法將給定的結(jié)構(gòu)序列化為JSON到響應(yīng)體, 并設(shè)置內(nèi)容類型Content-Type為:"application/json" 
  32.     c.JSON(http.StatusOK, msg) 
  33.   }) 
  34.  
  35.   r.GET("/someXML", func(c *gin.Context) { 
  36.     c.XML(http.StatusOK, gin.H{"message""hey""status": http.StatusOK}) 
  37.   }) 
  38.  
  39.   r.GET("/someYAML", func(c *gin.Context) { 
  40.     c.YAML(http.StatusOK, gin.H{"message""hey""status": http.StatusOK}) 
  41.   }) 
  42.  
  43.   //Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. 
  44.   //Protocol buffers(簡(jiǎn)稱ProtoBuf)是來(lái)自Google的一個(gè)跨語(yǔ)言,跨平臺(tái),用于將結(jié)構(gòu)化數(shù)據(jù)序列化的可擴(kuò)展機(jī)制, 
  45.   //詳見(jiàn):https://developers.google.com/protocol-buffers 
  46.   r.GET("/someProtoBuf", func(c *gin.Context) { 
  47.     reps := []int64{int64(1), int64(2)} 
  48.     label := "test" 
  49.     // The specific definition of protobuf is written in the testdata/protoexample file. 
  50.     // 使用protoexample.Test這個(gè)特別的protobuf結(jié)構(gòu)來(lái)定義測(cè)試數(shù)據(jù) 
  51.     data := &protoexample.Test{ 
  52.       Label: &label, 
  53.       Reps:  reps, 
  54.     } 
  55.     // Note that data becomes binary data in the response  //將data序列化為二進(jìn)制的響應(yīng)數(shù)據(jù) 
  56.     // Will output protoexample.Test protobuf serialized data 
  57.     // ProtoBuf serializes the given struct as ProtoBuf into the response body. 
  58.     // ProtoBuf方法將給定的結(jié)構(gòu)序列化為ProtoBuf響應(yīng)體 
  59.     c.ProtoBuf(http.StatusOK, data) 
  60.   }) 
  61.  
  62.   // Listen and serve on 0.0.0.0:8080 
  63.   r.Run(":8080"
  64.  
  65. /* 
  66. 模擬測(cè)試 
  67. curl http://localhost:8080/someJSON 
  68. {"message":"hey","status":200} 
  69.  
  70. curl http://localhost:8080/moreJSON 
  71. {"user":"Lena","Message":"hey","Number":123} 
  72.  
  73. curl http://localhost:8080/someXML 
  74. <map><message>hey</message><status>200</status></map> 
  75.  
  76. curl http://localhost:8080/someYAML 
  77. message: hey 
  78. status: 200 
  79.  
  80. curl http://localhost:8080/someProtoBuf 
  81. test 
  82. */ 

安全的JSOn

使用SecureJSON方法保護(hù)Json不被劫持, 如果響應(yīng)體是一個(gè)數(shù)組, 該方法會(huì)默認(rèn)添加`while(1)`前綴到響應(yīng)頭, 這樣的死循環(huán)可以防止后面的代碼被惡意執(zhí)行, 也可以自定義安全JSON的前綴.

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "net/http" 
  6.  
  7. func main() { 
  8.   r := gin.Default() 
  9.  
  10.   // You can also use your own secure json prefix 
  11.   // 你也可以自定義安全Json的前綴 
  12.   r.SecureJsonPrefix(")]}',\n"
  13.  
  14.   //使用SecureJSON方法保護(hù)Json不被劫持, 如果響應(yīng)體是一個(gè)數(shù)組, 該方法會(huì)默認(rèn)添加`while(1)`前綴到響應(yīng)頭,  這樣的死循環(huán)可以防止后面的代碼被惡意執(zhí)行, 也可以自定義安全JSON的前綴. 
  15.   r.GET("/someJSON", func(c *gin.Context) { 
  16.     names := []string{"lena""austin""foo"
  17.  
  18.     //names := map[string]string{ 
  19.     //  "hello""world"
  20.     //} 
  21.  
  22.     // Will output  :   while(1);["lena","austin","foo"
  23.     c.SecureJSON(http.StatusOK, names) 
  24.   }) 
  25.  
  26.   // Listen and serve on 0.0.0.0:8080 
  27.   r.Run(":8080"
  28.  
  29. /* 
  30. 模擬請(qǐng)求:curl http://localhost:8080/someJSON 
  31. )]}', 
  32. ["lena","austin","foo"]% 
  33. */ 

JSONP

使用JSONP可以實(shí)現(xiàn)跨域請(qǐng)求數(shù)據(jù), 如果請(qǐng)求中有查詢字符串參數(shù)callback, 則將返回?cái)?shù)據(jù)作為參數(shù)傳遞給callback值(前端函數(shù)名),整體作為一個(gè)響應(yīng)體,返回給前端.

JSONP是服務(wù)器與客戶端跨源通信的常用方法. 最大特點(diǎn)就是簡(jiǎn)單適用, 老式瀏覽器全部支持, 服務(wù)器改造非常小, 它的基本思想是: 網(wǎng)頁(yè)通過(guò)添加一個(gè)<script>元素, 向服務(wù)器請(qǐng)求JSON數(shù)據(jù), 這種做法不受同源政策限制, 服務(wù)器收到請(qǐng)求后, 將數(shù)據(jù)放在一個(gè)指定名字的回調(diào)函數(shù)里傳回來(lái), 這樣, 前端可以完成一次前端函數(shù)的調(diào)用, 而參數(shù)是后端返回的數(shù)據(jù).

注意: 這種方式存在被劫持的風(fēng)險(xiǎn)

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "net/http" 
  6.  
  7. func main() { 
  8.   r := gin.Default() 
  9.  
  10.   r.GET("/JSONP", func(c *gin.Context) { 
  11.     data := gin.H{ 
  12.       "foo""bar"
  13.     } 
  14.  
  15.     //callback is x 
  16.     // Will output  :   x({\"foo\":\"bar\"}) 
  17.     // 使用JSONP可以實(shí)現(xiàn)跨域請(qǐng)求數(shù)據(jù), 如果請(qǐng)求中有查詢字符串參數(shù)callback, 則將返回?cái)?shù)據(jù)作為參數(shù)傳遞給callback值(前端函數(shù)名),整體作為一個(gè)響應(yīng)體,返回給前端 
  18.     //JSONP是服務(wù)器與客戶端跨源通信的常用方法。最大特點(diǎn)就是簡(jiǎn)單適用,老式瀏覽器全部支持,服務(wù)器改造非常小。 
  19.     //它的基本思想是,網(wǎng)頁(yè)通過(guò)添加一個(gè)<script>元素,向服務(wù)器請(qǐng)求JSON數(shù)據(jù),這種做法不受同源政策限制;服務(wù)器收到請(qǐng)求后,將數(shù)據(jù)放在一個(gè)指定名字的回調(diào)函數(shù)里傳回來(lái) 
  20.     c.JSONP(http.StatusOK, data) 
  21.   }) 
  22.  
  23.   // Listen and serve on 0.0.0.0:8080 
  24.   r.Run(":8080"
  25.  
  26.   // 模擬客戶端,請(qǐng)求參數(shù)中有callback參數(shù),值為x(前端函數(shù)名),最后響應(yīng)內(nèi)容為x("foo":"bar"
  27.   // curl http://127.0.0.1:8080/JSONP?callback=x 

AsciiJSON

使用ASCII編碼, 將非ASCII的字符進(jìn)行轉(zhuǎn)義和編碼, 生成純ASCII編碼的JSON

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "net/http" 
  6.  
  7. func main() { 
  8.   r := gin.Default() 
  9.  
  10.   r.GET("/someJSON", func(c *gin.Context) { 
  11.     data := gin.H{ 
  12.       "lang""GO語(yǔ)言"
  13.       "tag":  "<br>"
  14.     } 
  15.  
  16.     // 輸出結(jié)果 : {"lang":"GO\u8bed\u8a00","tag":"\u003cbr\u003e"
  17.     // AsciiJSON方法返回帶有Unicode編碼和轉(zhuǎn)義組成的純ASCII字符串 
  18.     c.AsciiJSON(http.StatusOK, data) 
  19.   }) 
  20.  
  21.   // Listen and serve on 0.0.0.0:8080 
  22.   r.Run(":8080"
  23.  
  24. /* 
  25. 模擬請(qǐng)求:curl http://localhost:8080/someJSON 
  26.  */ 

不帶轉(zhuǎn)義的原始JSON

通常, JSON會(huì)將特殊的HTML字符轉(zhuǎn)化為他們的unicode編碼, 如標(biāo)簽`<`轉(zhuǎn)為`\u003c` 使用PureJSON方法可以得到原始不做轉(zhuǎn)義的字符串.

注意: 該方法至少需要Go版本1.6以上

  1. package main 
  2.  
  3. import "github.com/gin-gonic/gin" 
  4.  
  5. func main() { 
  6.   r := gin.Default() 
  7.  
  8.   // Serves unicode entities 
  9.   r.GET("/json", func(c *gin.Context) { 
  10.     c.JSON(200, gin.H{ 
  11.       "html""<b>Hello, world!</b>"
  12.     }) 
  13.   }) 
  14.  
  15.   // Serves literal characters 
  16.   r.GET("/purejson", func(c *gin.Context) { 
  17.     c.PureJSON(200, gin.H{ 
  18.       "html""<b>Hello, world!</b>"
  19.     }) 
  20.   }) 
  21.  
  22.   // listen and serve on 0.0.0.0:8080 
  23.   r.Run(":8080"
  24. /* 
  25. 模擬請(qǐng)求,得到將HTML標(biāo)簽轉(zhuǎn)義后的JSON字符串 
  26. curl http://localhost:8080/json 
  27. {"html":"\u003cb\u003eHello, world!\u003c/b\u003e"}                                                                                                                                                                            
  28. 得到原始JSON字符串 
  29. curl http://localhost:8080/purejson 
  30. {"html":"<b>Hello, world!</b>"
  31. */ 

參考文檔

Gin官方倉(cāng)庫(kù):https://github.com/gin-gonic/gin

[[355250]]

 

責(zé)任編輯:姜華 來(lái)源: 云原生云
相關(guān)推薦

2020-12-03 09:28:05

Golang GinW

2021-09-30 07:26:15

YamlJsonXml

2020-11-23 10:48:39

Golang GinW

2020-11-26 10:08:17

Golang GinW

2020-12-08 12:05:48

Golang GinW框架HTTPS

2020-11-27 07:54:53

Golang GinW

2019-03-29 08:25:21

JSONXML前端

2019-05-15 09:08:02

XMLJSONJDBC byte

2020-11-25 09:18:15

Golang GinW

2020-12-10 10:22:48

GinWeb中間件HTTPS

2020-11-25 09:10:39

Golang GinW

2012-05-16 13:53:25

brainyJava

2023-10-22 20:20:37

FiberGo

2009-08-05 16:32:25

Smooks 1.2框

2009-09-14 16:46:15

LINQ to XML

2019-02-11 08:48:07

XMLJSON前端

2024-04-22 13:31:20

2021-08-03 06:57:36

Protocol Bu平臺(tái)Json

2009-08-18 17:39:12

JSON.NET

2025-02-10 09:57:23

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)