mirror of
https://github.com/kataras/iris.git
synced 2025-12-24 05:17:03 +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:
65
_examples/http_request/read-custom-per-type/main.go
Normal file
65
_examples/http_request/read-custom-per-type/main.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// use Postman or whatever to do a POST request
|
||||
// (however you are always free to use app.Get and GET http method requests to read body of course)
|
||||
// to the http://localhost:8080 with RAW BODY:
|
||||
/*
|
||||
addr: localhost:8080
|
||||
serverName: Iris
|
||||
*/
|
||||
//
|
||||
// The response should be:
|
||||
// Received: main.config{Addr:"localhost:8080", ServerName:"Iris"}
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Post("/", handler)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
// simple yaml stuff, read more at https://github.com/go-yaml/yaml
|
||||
type config struct {
|
||||
Addr string `yaml:"addr"`
|
||||
ServerName string `yaml:"serverName"`
|
||||
}
|
||||
|
||||
// Decode implements the `kataras/iris/context#BodyDecoder` optional interface
|
||||
// that any go type can implement in order to be self-decoded when reading the request's body.
|
||||
func (c *config) Decode(body []byte) error {
|
||||
return yaml.Unmarshal(body, c)
|
||||
}
|
||||
|
||||
func handler(ctx iris.Context) {
|
||||
var c config
|
||||
|
||||
//
|
||||
// Note:
|
||||
// second parameter is nil because our &c implements the `context#BodyDecoder`
|
||||
// which has a priority over the context#Unmarshaler (which can be a more global option for reading request's body)
|
||||
// see the `http_request/read-custom-via-unmarshaler/main.go` example to learn how to use the context#Unmarshaler too.
|
||||
//
|
||||
// Note 2:
|
||||
// If you need to read the body again for any reason
|
||||
// you should disable the body consumption via `app.Run(..., iris.WithoutBodyConsumptionOnUnmarshal)`.
|
||||
//
|
||||
|
||||
if err := ctx.UnmarshalBody(&c, nil); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.WriteString(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("Received: %#+v", c)
|
||||
}
|
||||
17
_examples/http_request/read-custom-per-type/main_test.go
Normal file
17
_examples/http_request/read-custom-per-type/main_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
func TestReadCustomPerType(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
expectedResponse := `Received: main.config{Addr:"localhost:8080", ServerName:"Iris"}`
|
||||
|
||||
e.POST("/").WithText("addr: localhost:8080\nserverName: Iris").Expect().
|
||||
Status(httptest.StatusOK).Body().Equal(expectedResponse)
|
||||
}
|
||||
73
_examples/http_request/read-custom-via-unmarshaler/main.go
Normal file
73
_examples/http_request/read-custom-via-unmarshaler/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// use Postman or whatever to do a POST request
|
||||
// (however you are always free to use app.Get and GET http method requests to read body of course)
|
||||
// to the http://localhost:8080 with RAW BODY:
|
||||
/*
|
||||
addr: localhost:8080
|
||||
serverName: Iris
|
||||
*/
|
||||
//
|
||||
// The response should be:
|
||||
// Received: main.config{Addr:"localhost:8080", ServerName:"Iris"}
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Post("/", handler)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
// simple yaml stuff, read more at https://github.com/go-yaml/yaml
|
||||
type config struct {
|
||||
Addr string `yaml:"addr"`
|
||||
ServerName string `yaml:"serverName"`
|
||||
}
|
||||
|
||||
/*
|
||||
type myBodyDecoder struct{}
|
||||
|
||||
var DefaultBodyDecoder = myBodyDecoder{}
|
||||
|
||||
// Implements the `kataras/iris/context#Unmarshaler` but at our example
|
||||
// we will use the simplest `context#UnmarshalerFunc` to pass just the yaml.Unmarshal.
|
||||
//
|
||||
// Can be used as: ctx.UnmarshalBody(&c, DefaultBodyDecoder)
|
||||
func (r *myBodyDecoder) Unmarshal(data []byte, outPtr interface{}) error {
|
||||
return yaml.Unmarshal(data, outPtr)
|
||||
}
|
||||
*/
|
||||
|
||||
func handler(ctx iris.Context) {
|
||||
var c config
|
||||
|
||||
//
|
||||
// Note:
|
||||
// yaml.Unmarshal already implements the `context#Unmarshaler`
|
||||
// so we can use it directly, like the json.Unmarshal(ctx.ReadJSON), xml.Unmarshal(ctx.ReadXML)
|
||||
// and every library which follows the best practises and is aligned with the Go standards.
|
||||
//
|
||||
// Note 2:
|
||||
// If you need to read the body again for any reason
|
||||
// you should disable the body consumption via `app.Run(..., iris.WithoutBodyConsumptionOnUnmarshal)`.
|
||||
//
|
||||
|
||||
if err := ctx.UnmarshalBody(&c, iris.UnmarshalerFunc(yaml.Unmarshal)); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.WriteString(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("Received: %#+v", c)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
func TestReadCustomViaUnmarshaler(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
expectedResponse := `Received: main.config{Addr:"localhost:8080", ServerName:"Iris"}`
|
||||
|
||||
e.POST("/").WithText("addr: localhost:8080\nserverName: Iris").Expect().
|
||||
Status(httptest.StatusOK).Body().Equal(expectedResponse)
|
||||
}
|
||||
@@ -11,16 +11,18 @@ type Company struct {
|
||||
}
|
||||
|
||||
func MyHandler(ctx iris.Context) {
|
||||
c := &Company{}
|
||||
if err := ctx.ReadJSON(c); err != nil {
|
||||
var c Company
|
||||
|
||||
if err := ctx.ReadJSON(&c); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.WriteString(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("Received: %#v\n", c)
|
||||
ctx.Writef("Received: %#+v\n", c)
|
||||
}
|
||||
|
||||
// simple json stuff, read more at https://golang.org/pkg/encoding/json
|
||||
type Person struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
@@ -55,9 +57,9 @@ func main() {
|
||||
"Other": "Something here"
|
||||
}
|
||||
*/
|
||||
// and Content-Type to application/json
|
||||
// and Content-Type to application/json (optionally but good practise)
|
||||
//
|
||||
// The response should be:
|
||||
// Received: &main.Company{Name:"iris-Go", City:"New York", Other:"Something here"}
|
||||
app.Run(iris.Addr(":8080"))
|
||||
// Received: main.Company{Name:"iris-Go", City:"New York", Other:"Something here"}
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
|
||||
}
|
||||
|
||||
50
_examples/http_request/read-xml/main.go
Normal file
50
_examples/http_request/read-xml/main.go
Normal 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)
|
||||
}
|
||||
18
_examples/http_request/read-xml/main_test.go
Normal file
18
_examples/http_request/read-xml/main_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
func TestReadXML(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
expectedResponse := `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."}`
|
||||
send := `<person name="Winston Churchill" age="90"><description>Description of this person, the body of this inner element.</description></person>`
|
||||
|
||||
e.POST("/").WithText(send).Expect().
|
||||
Status(httptest.StatusOK).Body().Equal(expectedResponse)
|
||||
}
|
||||
Reference in New Issue
Block a user