mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 10:57:05 +00:00
add a new 'Context.GzipReader(bool) method and 'iris.GzipReader' middleware as requested at #1528
Former-commit-id: 7665545069bf1784d17a9db1e5f9f5f8df4b0c43
This commit is contained in:
44
_examples/http_request/read-gzip/main.go
Normal file
44
_examples/http_request/read-gzip/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
type payload struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
// GzipReader is a middleware which enables gzip decompression,
|
||||
// when client sends gzip compressed data.
|
||||
//
|
||||
// A shortcut of:
|
||||
// func(ctx iris.Context) {
|
||||
// ctx.GzipReader(true)
|
||||
// ctx.Next()
|
||||
// }
|
||||
app.Use(iris.GzipReader)
|
||||
|
||||
app.Post("/", func(ctx iris.Context) {
|
||||
// Bind incoming gzip compressed JSON to "p".
|
||||
var p payload
|
||||
if err := ctx.ReadJSON(&p); err != nil {
|
||||
ctx.StopWithError(iris.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send back the message as plain text.
|
||||
ctx.WriteString(p.Message)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user