Add support for Protobuf format response and unit test (#1479)
`Gin` now have the `protobufBinding` function to check the request format, but didn't have a protobuf response function like `c.YAML()`. In our company [ByteDance](http://bytedance.com/), the largest internet company using golang in China, we use `gin` to transfer __Protobuf__ instead of __Json__, we have to write some internal library to make some wrappers to achieve that, and the code is not elegant. So we really want such a feature.
This commit is contained in:
17
README.md
17
README.md
@ -40,7 +40,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
||||
- [Bind Query String or Post Data](#bind-query-string-or-post-data)
|
||||
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
||||
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
||||
- [XML, JSON and YAML rendering](#xml-json-and-yaml-rendering)
|
||||
- [XML, JSON, YAML and ProtoBuf rendering](#xml-json-yaml-and-protobuf-rendering)
|
||||
- [JSONP rendering](#jsonp)
|
||||
- [Serving static files](#serving-static-files)
|
||||
- [Serving data from reader](#serving-data-from-reader)
|
||||
@ -871,7 +871,7 @@ Test it with:
|
||||
$ curl -v --form user=user --form password=password http://localhost:8080/login
|
||||
```
|
||||
|
||||
### XML, JSON and YAML rendering
|
||||
### XML, JSON, YAML and ProtoBuf rendering
|
||||
|
||||
```go
|
||||
func main() {
|
||||
@ -905,6 +905,19 @@ func main() {
|
||||
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
||||
})
|
||||
|
||||
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.
|
||||
data := &protoexample.Test{
|
||||
Label: &label,
|
||||
Reps: reps,
|
||||
}
|
||||
// Note that data becomes binary data in the response
|
||||
// Will output protoexample.Test protobuf serialized data
|
||||
c.ProtoBuf(http.StatusOK, data)
|
||||
})
|
||||
|
||||
// Listen and serve on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
Reference in New Issue
Block a user