Gin 框架学习笔记(03)— 输出响应与渲染
在 Gin 框架中,對 HTTP 請求可以很方便有多種不同形式的響應。比如響應為 JSON 、 XML 或者是 HTML 等。
?
Context 的以下方法在 Gin 框架中把內容序列化為不同類型寫入到響應主體中。
// HTML 呈現指定文件名的 HTTP 模板。
// 更新 HTTP 狀態代碼并將 Content-Type設置為 “text/html”。
// 參見 http://golang.org/doc/articles/wiki/
func (c *Context) HTML(code int, name string, obj interface{})// IndentedJSON 將給定結構序列化為漂亮的 JSON(縮進+結束行)到響應體中。
// 同時將 Content-Type設置為 “application/json”。
// 警告:建議僅將此用于開發目的,因為打印漂亮的 JSON 會占用更多 CPU 和帶寬。
// 請改用 Context.JSON() 。
func (c *Context) IndentedJSON(code int, obj interface{})// SecureJSON 將給定結構序列化為安全 JSON 到響應主體中。
// 如果給定的結構是數組值,則默認將 “while(1)” 添加到響應主體。
// 將 Content-Type設置為 “application/json” 。
func (c *Context) SecureJSON(code int, obj interface{})// JSONP 將給定結構序列化為 JSON 到響應體中。
// 它可跨域向服務器請求數據。
// 將 Content-Type 設置為 “application/javascript” 。
func (c *Context) JSONP(code int, obj interface{})// JSON 將給定結構序列化為 JSON 到響應主中。
// 將 Content-Type 設置為 “application/json” 。
func (c *Context) JSON(code int, obj interface{})// AsciiJSON 將給定結構作為 JSON 序列化到響應體中,并將 Unicode 序列化為 ASCII 字符串。
// 將 Content-Type 設置為 “application/json” 。
func (c *Context) AsciiJSON(code int, obj interface{})// PureJSON 將給定結構序列化為 JSON 到響應體中。
// 與 JSON 不同,PureJSON 不會用他們的 Unicode 實體替換特殊的 HTML 字符。
func (c *Context) PureJSON(code int, obj interface{})// XML 將給定結構序列化為 XML 到響應體中。
// 將 Content-Type 設置為 “application/xml” 。
func (c *Context) XML(code int, obj interface{})// YAML 將給定結構序列化為 YAML 到響應體中。
func (c *Context) YAML(code int, obj interface{})// ProtoBuf 將給定結構序列化為 ProtoBuf 到響應體中。
func (c *Context) ProtoBuf(code int, obj interface{})// String 將給定字符串寫入到響應體中。
func (c *Context) String(code int, format string, values ...interface{})// Redirect 將 HTTP 重定向返回到特定位置。
func (c *Context) Redirect(code int, location string)// Data 將一些數據寫入正文流并更新 HTTP 狀態代碼。
func (c *Context) Data(code int, contentType string, data []byte)// DataFromReader 將指定的讀取器寫入正文流并更新 HTTP 代碼。
func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)// File 以有效的方式將指定的文件寫入正文流。
func (c *Context) File(filepath string)// FileAttachment 以有效的方式將指定的文件寫入正文流中在客戶端,
// 通常會使用給定的文件名下載該文件。
func (c *Context) FileAttachment(filepath, filename string)// SSEvent 將 Server-Sent 事件寫入正文流。
func (c *Context) SSEvent(name string, message interface{})// Stream 發送流響應并返回布爾值,表示“客戶端在流中間斷開連接”。
func (c *Context) Stream(step func(w io.Writer) bool) bool
1. XML/JSON/YAML/ProtoBuf 響應
這些響應的方法可以把數據序列化為對應數據編碼方式。
func main() {router := gin.Default()router.GET("/someJSON", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"userinfo": "Mo", "status": http.StatusOK})})router.GET("/moreJSON", func(c *gin.Context) {// 也可使用一個結構體var msg struct {Name string `json:"user"`UserInfo stringNumber int}msg.Name = "Lena"msg.UserInfo = "Mo"msg.Number = 123// 注意 msg.Name 在 JSON 中變成了 "user"c.JSON(http.StatusOK, msg)})router.GET("/someXML", func(c *gin.Context) {c.XML(http.StatusOK, gin.H{"userinfo": "Mo", "status": http.StatusOK})})router.GET("/someYAML", func(c *gin.Context) {c.YAML(http.StatusOK, gin.H{"userinfo": "Mo", "status": http.StatusOK})})router.GET("/someProtoBuf", func(c *gin.Context) {name := "Lena"// protobuf 的具體定義寫在 pb/user文件中。data := &pb.UserInfo{UserType: 101,UserName: name,UserInfo: "Mo",}// 將輸出被 protobuf 序列化了的數據c.ProtoBuf(http.StatusOK, data)})// 監聽并啟動服務router.Run(":8080")
}
程序運行在 Debug 模式時,在命令行運行下面命令:
curl -v http://localhost:8080/someProtoBuf
命令行返回:
> GET /someProtoBuf HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/x-protobuf
< Date: Sun, 14 Jul 2019 10:13:42 GMT
< Content-Length: 12
<
繼續運行命令:
curl -v http://localhost:8080/someYAML
命令行返回:
> GET /someYAML HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/x-yaml; charset=utf-8
< Date: Sun, 14 Jul 2019 10:13:45 GMT
< Content-Length: 25
<
status: 200
userinfo: Mo
繼續保持程序運行,通過瀏覽器訪問 http://localhost:8080/moreJSON ,頁面顯示:
{"user":"Lena","UserInfo":"Mo","Number":123}
通過瀏覽器訪問 http://localhost:8080/someJSON ,頁面顯示:
{"status":200,"userinfo":"Mo"}
通過瀏覽器訪問 http://localhost:8080/someXML ,頁面顯示:
<map>
<userinfo>Mo</userinfo>
<status>200</status>
</map>
從以上命令行運行命令情況以及瀏覽器訪問顯示,以及控制臺輸出可以看到 Gin 框架可以根據需要,把內容序列化為不同 Content-Type 類型寫入到響應體。
?
下面以 c.JSON() 方法為例,來說說這類方法實現渲染的過程,下面是 c.JSON() 方法的定義:
func (c *Context) JSON(code int, obj interface{}) {c.Render(code, render.JSON{Data: obj})
}
在 c.Render() 方法中,第二個參數是結構體對象 render.JSON{Data: obj} ,這個結構體定義在 render/json.go 文件中,并且這個結構體還有兩個方法:
// Render (JSON) writes data with custom ContentType.
func (r JSON) Render(w http.ResponseWriter) (err error) {if err = WriteJSON(w, r.Data); err != nil {panic(err)}return
}// WriteContentType (JSON) writes JSON ContentType.
func (r JSON) WriteContentType(w http.ResponseWriter) {writeContentType(w, jsonContentType)
}
這兩個方法實現了在 render/render.go 中定義的接口:
type Render interface {// Render writes data with custom ContentType.Render(http.ResponseWriter) error// WriteContentType writes custom ContentType.WriteContentType(w http.ResponseWriter)
}
所以,程序可以通過 c.Render() 這個渲染的通用方法來適配不同的渲染器,因為其第二個參數對應著 render 目錄下的眾多渲染器,比如 JSONP 、 HTML 等等。這些渲染器都實現了接口 Render ,所以都可以通過 c.Render() 方法來把渲染后的結果響應給客戶端。
?
2. JSONP 響應
JSONP 可以向不同域的服務器跨域請求數據。如果查詢參數存在回調,則將回調添加到響應體中。在 Gin 框架中,支持 JSONP 響應,下面分兩部分來說明 Gin 框架中 JSONP 的使用。
?
服務端輸出 JSONP:
func main() {router := gin.Default()router.GET("/jsonp", func(c *gin.Context) {data := map[string]interface{}{"foo": "bar",}// callback 是 x// 將輸出:x({\\"foo\\":\\"bar\\"})c.JSONP(http.StatusOK, data)})// 監聽并啟動服務router.Run(":8080")
}
客戶端調用:
<div id="divJsonp"></div>
<script>
$.getJSON("<http://localhost:8080/jsonp?callback=?">, function(data) {var html = '<ul>';html += '<li>' + data["foo"] + '</li>';html += '</ul>';$('#divJsonp').html(html);
});
</script>
首先把服務端運行在 Debug 模式,為了模擬跨域,這里把客戶端所在目錄作為 Web 服務在 8081 端口運行起來。可在瀏覽器訪問:http://localhost:8081/j.html ,頁面顯示:
- bar
從控制臺輸出可以看到服務端正常接收了 jsonp 調用,且瀏覽器頁面能正常展示服務端響應的內容。
3. SecureJSON 響應
SecureJSON() 方法將給定結構序列化為安全 JSON 到響應主體中。如果給定的結構是數組值,則默認將 while (1) 添加到響應主體。該響應方法會將 Content-Type 設置為 application/json 。使用 SecureJSON() 方法來防止 JSON劫持。
func main() {router := gin.Default()// 可以自定義添加安全 JSON 前綴// router.SecureJsonPrefix(")]}',\\n")router.GET("/someJSON", func(c *gin.Context) {names := []string{"lena", "austin", "foo"}// SecureJsonPrefix() 設置優先,// 否則將會輸出: while(1);["lena","austin","foo"]c.SecureJSON(http.StatusOK, names)})// 監聽并啟動服務router.Run(":8080")
}
運行程序,通過瀏覽器訪問 http://localhost:8080/someJSON ,頁面顯示:
while(1);["lena","austin","foo"]
如自定義設置安全前綴 router.SecureJsonPrefix(")]}',\\n"),則頁面顯示:)]}', ["lena","austin","foo"] 。
這些添加前綴的方法對提升數據的讀安全有較好的防護作用,在開發中建議要增強數據安全意識,避免不必要安全問題的發生。
4. AsciiJSON 響應
AsciiJSON() 方法將給定結構作為 JSON 序列化到響應體中,并將 Unicode 序列化為 ASCII 字符串。該響應方法會將 Content-Type 設置為 application/json 。
func main() {router := gin.Default()router.GET("/someJSON", func(c *gin.Context) {data := gin.H{"lang": "GO語言","tag": "<br>",}// 輸出 : {"lang":"GO\\u8bed\\u8a00","tag":"\\u003cbr\\u003e"}c.AsciiJSON(http.StatusOK, data)})// 監聽并啟動服務router.Run(":8080")
}
運行程序,通過瀏覽器訪問 http://localhost:8080/someJSON ,頁面顯示:
{"lang":"GO\\u8bed\\u8a00","tag":"\\u003cbr\\u003e"}
5. PureJSON 響應
PureJSON() 方法將給定結構序列化為 JSON 到響應體中。與 JSON 不同,PureJSON() 方法不會用 Unicode 實體替換特殊的 HTML 字符。
func main() {router := gin.Default()// Unicode 實體router.GET("/json", func(c *gin.Context) {c.JSON(200, gin.H{"html": "<b>Hello, world!</b>",})})// 純字符標識router.GET("/purejson", func(c *gin.Context) {c.PureJSON(200, gin.H{"html": "<b>Hello, world!</b>",})})// 監聽并啟動服務router.Run(":8080")
}
運行程序,通過瀏覽器訪問 http://localhost:8080/json ,頁面顯示:
{"html":"\\u003cb\\u003eHello, world!\\u003c/b\\u003e"}
訪問 http://localhost:8080/purejson ,頁面顯示:
{"html":"<b>Hello, world!</b>"}
通過對比可以知道, JSON() 方法與 PureJSON() 方法的區別在于,
PureJSON()不會對內容進行任何的編碼轉義處理JSON()以及AsciiJSON()方法都會對內容進行變碼轉義處理SecureJSON()方法甚至會額外添加其他內容。
但不管怎么,這些方法都有其使用場景,為開發人員提供了更多便利的選擇。
6. DataFromReader 響應
DataFromReader() 方法將指定的讀取器寫入正文流并更新 HTTP 狀態碼。也就是從數據流讀取數據后處理并更新 HTTP 狀態碼。
func main() {router := gin.Default()router.GET("/someDataFromReader", func(c *gin.Context) {response, err := http.Get("<https://raw.githubusercontent.com/gin-gonic/logo/master/color.png>")if err != nil || response.StatusCode != http.StatusOK {c.Status(http.StatusServiceUnavailable)return}reader := response.BodycontentLength := response.ContentLengthcontentType := response.Header.Get("Content-Type")extraHeaders := map[string]string{// 做為附件下載保存// "Content-Disposition": `attachment; filename="gopher.png"`,// 直接在瀏覽器顯示"Content-Type": "image/png",}c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)})router.GET("/file", func(c *gin.Context) {// 輸出當前目錄 main.go 文件內容c.File("main.go")})router.GET("/attach", func(c *gin.Context) {// 下載當前目錄 main.go 文件為 test-main.goc.FileAttachment("./main.go", "test-main.go")})router.Run(":8080")
}
運行程序,通過瀏覽器訪問 http://localhost:8080/someDataFromReader ,程序會讀取 https://raw.githubusercontent.com/gin-gonic/logo/master/color.png 這張圖,然后在頁面顯示這張圖,這個過程并不是 HTML 標簽來完成,而是通過程序來讀取圖片數據然后設置 “Content-Type”: "image/png " 才呈現出來的。
在上面代碼中,如果 extraHeaders 設置為第一種則會作為附件下載保存這張圖片。可以這樣理解這個方法:可以從任何 Reader 讀取數據并按 extraHeaders 的配置來響應渲染內容。
而為了更多演示說明有關文件處理,可通過瀏覽器訪問 http://localhost:8080/file ,會直接在瀏覽器中顯示源文件代碼,另外通過瀏覽器訪問 http://localhost:8080/attach ,會在瀏覽器中下載保存源文件 main.go 為 test-main.go 文件。這兩個方法 File() 和 FileAttachment() 不會經常使用,但比較有用。
7. Redirect 重定向
重定向也可以認為是響應的一種方式。這里列舉了兩種方式,一種是常見的 301 重定向,還有一種是其實是 Gin 的 Handler 重新指定,通過 HandleContex() 方法來重新調用其他的 Handler 。
func main() {router := gin.Default()// 直接重定向到外部站點router.GET("/re", func(c *gin.Context) {c.Redirect(http.StatusMovedPermanently, "http://www.baidu.com/")})// 間接重定向到內部 Handlerrouter.GET("/handle", func(c *gin.Context) {c.Request.URL.Path = "/hi"router.HandleContext(c)c.JSON(200, gin.H{"Handler": "handle"})})router.GET("/hi", func(c *gin.Context) {c.JSON(200, gin.H{"hello": "world"})})// 監聽并啟動服務router.Run(":8080")
}
程序運行在 Debug 模式時,通過瀏覽器訪問 http://localhost:8080/handle ,頁面顯示:
{"hello":"world"}
{"Handler":"handle"}
控制臺輸出:
[GIN-debug] GET /re --> main.main.func1 (3 handlers)
[GIN-debug] GET /handle --> main.main.func2 (3 handlers)
[GIN-debug] GET /hi --> main.main.func3 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2019/07/14 - 22:24:41 | 200 | 996.8μs | ::1 | GET /hi
[GIN] 2019/07/14 - 22:24:41 | 200 | 26.9291ms | ::1 | GET /handle
可以看到實際上執行了兩個 Handler 處理程序,響應狀態碼為: 200 ,并不是真正的重定向,但可以通過 HandleContex() 方法在程序中實現 Handler 的嵌套調用。
?
程序運行繼續運行,在命令行運行:
curl -v http://localhost:8080/re
命令行返回
> GET /re HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: <http://www.baidu.com/>
< Date: Sun, 14 Jul 2019 14:21:25 GMT
< Content-Length: 56
<
<a href="<http://www.baidu.com/>">Moved Permanently</a>.
返回的狀態碼為: 301 ,說明 Redirect() 方法實現了真正的重定向。而通過瀏覽器訪問 http://localhost:8080/re ,將會重定向打開百度首頁。
?
原文地址:https://gitbook.cn/gitchat/column/5dab061e7d66831b22aa0b44/topic/5dab09fd7d66831b22aa0b5e
總結
以上是生活随笔為你收集整理的Gin 框架学习笔记(03)— 输出响应与渲染的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022-2028年中国羽绒工业投资分析
- 下一篇: 2022-2028年中国领带行业投资分析