Golang GinWeb框架6-XMLJSON等渲染

簡介

本文接著上文(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/someXMLhey200​curl http://localhost:8080/someYAMLmessage: heystatus: 200​curl http://localhost:8080/someProtoBuftest*/

安全的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是伺服器與客戶端跨源通訊的常用方法。 最大特點就是簡單適用, 老式瀏覽器全部支援, 伺服器改造非常小, 它的基本思想是: 網頁透過新增一個