1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 18:37:05 +00:00

add examples for read using custom decoder per type, read using custom decoder via iris#UnmarshalerFunc and to complete it add an example for the context#ReadXML.

Former-commit-id: 536b1780f12d0b9d9ce9aa976a0f95f18634ec2d
This commit is contained in:
Gerasimos Maropoulos
2018-03-08 05:21:16 +02:00
parent 1a4803307d
commit 83c4b7f52d
14 changed files with 314 additions and 34 deletions

View File

@@ -0,0 +1,50 @@
package main
import (
"encoding/xml"
"github.com/kataras/iris"
)
func main() {
app := newApp()
// use Postman or whatever to do a POST request
// to the http://localhost:8080 with RAW BODY:
/*
<person name="Winston Churchill" age="90">
<description>Description of this person, the body of this inner element.</description>
</person>
*/
// and Content-Type to application/xml (optionally but good practise)
//
// The response should be:
// Received: main.person{XMLName:xml.Name{Space:"", Local:"person"}, Name:"Winston Churchill", Age:90, Description:"Description of this person, the body of this inner element."}
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
}
func newApp() *iris.Application {
app := iris.New()
app.Post("/", handler)
return app
}
// simple xml stuff, read more at https://golang.org/pkg/encoding/xml
type person struct {
XMLName xml.Name `xml:"person"` // element name
Name string `xml:"name,attr"` // ,attr for attribute.
Age int `xml:"age,attr"` // ,attr attribute.
Description string `xml:"description"` // inner element name, value is its body.
}
func handler(ctx iris.Context) {
var p person
if err := ctx.ReadXML(&p); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}
ctx.Writef("Received: %#+v", p)
}