mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
add Context.ReadJSONProtobuf method
Former-commit-id: 9c7f268c0d7be7e405ea97a5ac12257beba3a4f3
This commit is contained in:
60
_examples/response-writer/protobuf/main.go
Normal file
60
_examples/response-writer/protobuf/main.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"app/protos"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", send)
|
||||
app.Get("/json", sendAsJSON)
|
||||
app.Post("/read", read)
|
||||
app.Post("/read_json", readFromJSON)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func send(ctx iris.Context) {
|
||||
response := &protos.HelloReply{Message: "Hello, World!"}
|
||||
ctx.Protobuf(response)
|
||||
}
|
||||
|
||||
func sendAsJSON(ctx iris.Context) {
|
||||
response := &protos.HelloReply{Message: "Hello, World!"}
|
||||
options := iris.JSON{
|
||||
Proto: iris.ProtoMarshalOptions{
|
||||
AllowPartial: true,
|
||||
Multiline: true,
|
||||
Indent: " ",
|
||||
},
|
||||
}
|
||||
|
||||
ctx.JSON(response, options)
|
||||
}
|
||||
|
||||
func read(ctx iris.Context) {
|
||||
var request protos.HelloRequest
|
||||
|
||||
err := ctx.ReadProtobuf(&request)
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("HelloRequest.Name = %s", request.Name)
|
||||
}
|
||||
|
||||
func readFromJSON(ctx iris.Context) {
|
||||
var request protos.HelloRequest
|
||||
|
||||
err := ctx.ReadJSONProtobuf(&request)
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("HelloRequest.Name = %s", request.Name)
|
||||
}
|
||||
Reference in New Issue
Block a user