mirror of
https://github.com/kataras/iris.git
synced 2026-01-10 21:45:57 +00:00
add context.Protobuf, MsgPack, ReadProtobuf, ReadMsgPack methods
Former-commit-id: 39d547ecfb1516505a1eb76a12a1f6e9e4111962
This commit is contained in:
@@ -248,6 +248,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
|
||||
- [Read JSON](http_request/read-json/main.go)
|
||||
* [Struct Validation](http_request/read-json-struct-validation/main.go)
|
||||
- [Read XML](http_request/read-xml/main.go)
|
||||
- [Read MsgPack](http_request/read-msgpack/main.go) **NEW**
|
||||
- [Read YAML](http_request/read-yaml/main.go)
|
||||
- [Read Form](http_request/read-form/main.go)
|
||||
- [Read Query](http_request/read-query/main.go)
|
||||
|
||||
@@ -35,7 +35,7 @@ func main() {
|
||||
DisableBodyConsumptionOnUnmarshal: false,
|
||||
DisableAutoFireStatusCode: false,
|
||||
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
|
||||
Charset: "UTF-8",
|
||||
Charset: "utf-8",
|
||||
}))
|
||||
}
|
||||
```
|
||||
@@ -60,10 +60,10 @@ func main() {
|
||||
// Prefix: "With", code editors will help you navigate through all
|
||||
// configuration options without even a glitch to the documentation.
|
||||
|
||||
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
||||
|
||||
// or before run:
|
||||
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
||||
// app.Listen(":8080")
|
||||
}
|
||||
```
|
||||
@@ -76,7 +76,7 @@ EnablePathEscape = false
|
||||
FireMethodNotAllowed = true
|
||||
DisableBodyConsumptionOnUnmarshal = false
|
||||
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
|
||||
Charset = "UTF-8"
|
||||
Charset = "utf-8"
|
||||
|
||||
[Other]
|
||||
MyServerName = "iris"
|
||||
|
||||
@@ -21,7 +21,7 @@ func main() {
|
||||
DisableBodyConsumptionOnUnmarshal: false,
|
||||
DisableAutoFireStatusCode: false,
|
||||
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
|
||||
Charset: "UTF-8",
|
||||
Charset: "utf-8",
|
||||
}))
|
||||
|
||||
// or before Run:
|
||||
|
||||
@@ -3,7 +3,7 @@ EnablePathEscape = false
|
||||
FireMethodNotAllowed = true
|
||||
DisableBodyConsumptionOnUnmarshal = false
|
||||
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
|
||||
Charset = "UTF-8"
|
||||
Charset = "utf-8"
|
||||
|
||||
[Other]
|
||||
MyServerName = "iris"
|
||||
|
||||
@@ -15,9 +15,9 @@ func main() {
|
||||
// Prefix: "With", code editors will help you navigate through all
|
||||
// configuration options without even a glitch to the documentation.
|
||||
|
||||
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
||||
|
||||
// or before run:
|
||||
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
||||
// app.Listen(":8080")
|
||||
}
|
||||
|
||||
38
_examples/http_request/read-msgpack/main.go
Normal file
38
_examples/http_request/read-msgpack/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
// User example struct to bind to.
|
||||
type User struct {
|
||||
Firstname string `msgpack:"firstname"`
|
||||
Lastname string `msgpack:"lastname"`
|
||||
City string `msgpack:"city"`
|
||||
Age int `msgpack:"age"`
|
||||
}
|
||||
|
||||
// readMsgPack reads a `User` from MsgPack post body.
|
||||
func readMsgPack(ctx iris.Context) {
|
||||
var u User
|
||||
err := ctx.ReadMsgPack(&u)
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.WriteString(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("Received: %#+v\n", u)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Post("/", readMsgPack)
|
||||
|
||||
// POST: http://localhost:8080
|
||||
//
|
||||
// To run the example, use a tool like Postman:
|
||||
// 1. Body: Binary
|
||||
// 2. Select File, select the one from "_examples/http_responsewriter/write-rest" example.
|
||||
// The output should be:
|
||||
// Received: main.User{Firstname:"John", Lastname:"Doe", City:"Neither FBI knows!!!", Age:25}
|
||||
app.Listen(":8080")
|
||||
}
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
|
||||
// User bind struct
|
||||
// User example struct for json and msgpack.
|
||||
type User struct {
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
City string `json:"city"`
|
||||
Age int `json:"age"`
|
||||
Firstname string `json:"firstname" msgpack:"firstname"`
|
||||
Lastname string `json:"lastname" msgpack:"lastname"`
|
||||
City string `json:"city" msgpack:"city"`
|
||||
Age int `json:"age" msgpack:"age"`
|
||||
}
|
||||
|
||||
// ExampleXML just a test struct to view represents xml content-type
|
||||
@@ -22,6 +22,12 @@ type ExampleXML struct {
|
||||
Two string `xml:"two,attr"`
|
||||
}
|
||||
|
||||
// ExampleYAML just a test struct to write yaml to the client.
|
||||
type ExampleYAML struct {
|
||||
Name string `yaml:"name"`
|
||||
ServerAddr string `yaml:"ServerAddr"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
@@ -36,7 +42,7 @@ func main() {
|
||||
|
||||
// Write
|
||||
app.Get("/encode", func(ctx iris.Context) {
|
||||
peter := User{
|
||||
u := User{
|
||||
Firstname: "John",
|
||||
Lastname: "Doe",
|
||||
City: "Neither FBI knows!!!",
|
||||
@@ -44,7 +50,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Manually setting a content type: ctx.ContentType("application/javascript")
|
||||
ctx.JSON(peter)
|
||||
ctx.JSON(u)
|
||||
})
|
||||
|
||||
// Other content types,
|
||||
@@ -74,6 +80,21 @@ func main() {
|
||||
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
|
||||
})
|
||||
|
||||
app.Get("/yaml", func(ctx iris.Context) {
|
||||
ctx.YAML(ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"})
|
||||
})
|
||||
|
||||
app.Get("/msgpack", func(ctx iris.Context) {
|
||||
u := User{
|
||||
Firstname: "John",
|
||||
Lastname: "Doe",
|
||||
City: "Neither FBI knows!!!",
|
||||
Age: 25,
|
||||
}
|
||||
|
||||
ctx.MsgPack(u)
|
||||
})
|
||||
|
||||
// http://localhost:8080/decode
|
||||
// http://localhost:8080/encode
|
||||
//
|
||||
@@ -83,6 +104,7 @@ func main() {
|
||||
// http://localhost:8080/jsonp
|
||||
// http://localhost:8080/xml
|
||||
// http://localhost:8080/markdown
|
||||
// http://localhost:8080/msgpack
|
||||
//
|
||||
// `iris.WithOptimizations` is an optional configurator,
|
||||
// if passed to the `Run` then it will ensure that the application
|
||||
|
||||
@@ -83,7 +83,7 @@ func main() {
|
||||
})
|
||||
|
||||
// Listen for incoming HTTP/1.x & HTTP/2 clients on localhost port 8080.
|
||||
app.Listen(":8080", iris.WithCharset("UTF-8"))
|
||||
app.Listen(":8080", iris.WithCharset("utf-8"))
|
||||
}
|
||||
|
||||
func logThisMiddleware(ctx iris.Context) {
|
||||
|
||||
@@ -24,7 +24,7 @@ func main() {
|
||||
app.Get("/", hi)
|
||||
|
||||
// http://localhost:8080
|
||||
app.Listen(":8080", iris.WithCharset("UTF-8")) // defaults to that but you can change it.
|
||||
app.Listen(":8080", iris.WithCharset("utf-8")) // defaults to that but you can change it.
|
||||
}
|
||||
|
||||
func hi(ctx iris.Context) {
|
||||
|
||||
Reference in New Issue
Block a user