In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to master XML/JSON/YAML/ProtoBuf rendering in Golang GinWeb framework". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to master XML/JSON/YAML/ProtoBuf rendering in Golang GinWeb framework.
XML,JSON,YAML,ProtoBuf and so on render
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 object is a map mapping, the key name is string type, and the key value is interface, so all types r.GET ("/ someJSON") can be passed. 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". The / / JSON method serializes the given structure into a JSON to the response body, and sets the content type Content-Type to: "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) is a cross-language, cross-platform extensible mechanism from Google for serializing structured data. / / for details, see 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. / / use the special protobuf structure of protoexample.Test to define the test data data: = & protoexample.Test {Label: & label, Reps: reps,} / Note that data becomes binary data in the response / / serialize data into binary response data / / Will output protoexample.Test protobuf serialized data / / ProtoBuf serializes the given struct as ProtoBuf into the response body. / / ProtoBuf method serializes the given structure into ProtoBuf responder c.ProtoBuf (http.StatusOK, data)}) / / Listen and serve on 0.0.0.0ProtoBuf 8080 r.Run (": 8080")} / * Simulation test 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 hey200 curl http://localhost:8080/someYAML message: hey status: 200 curl http://localhost:8080/someProtoBuf test * /
Secure JSOn
Use the SecureJSON method to protect the Json from being hijacked. If the response body is an array, the method adds the prefix `while (1) `to the response header by default. Such a dead loop can prevent the following code from being executed maliciously, and the prefix of the secure JSON can also be customized.
Package main import ("github.com/gin-gonic/gin"net/http") func main () {r: = gin.Default () / / You can also use your own secure json prefix / / you can also customize the security Json prefix r.SecureJsonPrefix (")]}',\ n") / / use the SecureJSON method to protect Json from hijacking if the response body is an array This method adds the prefix `while (1) `to the response header by default. Such an endless loop can prevent the following code from being executed maliciously, and the prefix of the secure JSON can also be customized. 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.0austin 8080 r.Run (": 8080")} / * Simulation request: curl http://localhost:8080/someJSON)]}', ["lena", "austin", "foo"]% * /
JSONP
Cross-domain request data can be realized by using JSONP. If there is a query string parameter callback in the request, the returned data is passed to the callback value (front-end function name) as a parameter, and the whole is returned to the front-end as a response body.
JSONP is a common method of cross-source communication between server and client. The most important feature is simple and applicable, all the old browsers support, and the server transformation is very small. its basic idea is: the web page requests JSON data from the server by adding an element, which is not restricted by the same origin policy. after receiving the request, the server sends the data back in a callback function with a specified name, so that the front end can complete a call to the front end function. The parameters are the data returned by the backend.
Note: there is a risk of hijacking in this way
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\"}) / / you can use JSONP to request data across domains. If there is a query string parameter callback in the request, the returned data is passed as a parameter to the callback value (the name of the front-end function) as a whole as a response body Returning to the front end / / JSONP is a common method of cross-source communication between the server and the client. The biggest feature is simple and applicable, all the old browsers support it, and the server modification is very small. / / its basic idea is that the web page requests JSON data from the server by adding an element, which is not restricted by the same origin policy. After the server receives the request Send the data back to c.JSONP (http.StatusOK, data)}) / / Listen and serve on 0.0.0.0data 8080 r.Run (": 8080") / / simulated client in a callback function with a specified name. The request parameter contains the callback parameter with a value of x (the name of the front-end function). The final response content is x ("foo": "bar") / / curl http://127.0.0.1:8080/JSONP?callback=x}
AsciiJSON
Using ASCII encoding, non-ASCII characters are escaped and encoded to generate pure ASCII-encoded 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 language", "tag": "
",} / / output result: {" lang ":" GO\ u8bed\ u8a00 "," tag ":"\ u003cbr\ u003e "} / / AsciiJSON method returns a pure ASCII string c.AsciiJSON (http.StatusOK) with Unicode encoding and escape Data)}) / / Listen and serve on 0.0.0.0 Listen and serve on 8080 r.Run (": 8080")} / * Simulation request: curl http://localhost:8080/someJSON * /
Original JSON without escape
Typically, JSON converts special HTML characters into their unicode encodings, such as the label `
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.