-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
49 lines (40 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"encoding/xml"
"github.com/kataras/iris/v12"
)
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.Listen(":8080", 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.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Writef("Received: %#+v", p)
}