Skip to content

Commit

Permalink
update #82 新增响应 body demo
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Nov 9, 2019
1 parent b329ed1 commit 2814c35
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions _example/req_body.go → _example/04a-set-body.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
fmt.Printf("\n\n=====1.=====send string========\n")
err := gout.POST(":8080/req/body").
Debug(gout.DebugColor()).
SetBody("send string").
SetBody("send string"). // string
Do()

if err != nil {
Expand All @@ -38,7 +38,7 @@ func main() {
fmt.Printf("=====2.=====send string========\n")
err = gout.POST(":8080/req/body").
Debug(gout.DebugColor()).
SetBody([]byte("send bytes")).
SetBody([]byte("send bytes")). // []byte
Do()

if err != nil {
Expand All @@ -50,7 +50,7 @@ func main() {
fmt.Printf("=====3.=====io.Reader========\n")
err = gout.POST(":8080/req/body").
Debug(gout.DebugColor()).
SetBody(strings.NewReader("io.Reader")).
SetBody(strings.NewReader("io.Reader")). // io.Reader
Do()

if err != nil {
Expand All @@ -62,7 +62,7 @@ func main() {
fmt.Printf("=====4.=====base type========\n")
err = gout.POST(":8080/req/body").
Debug(gout.DebugColor()).
SetBody(3.14).
SetBody(3.14). //float64
Do()

if err != nil {
Expand Down
76 changes: 76 additions & 0 deletions _example/04b-bind-body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"time"
)

func server() {
router := gin.New()
router.GET("/rsp/body/bytes", func(c *gin.Context) {
c.String(200, "bytes")
})

router.GET("/rsp/body/string", func(c *gin.Context) {
c.String(200, "string")
})

router.GET("/rsp/body/int", func(c *gin.Context) {
c.String(200, "65535")
})

router.Run()
}

func main() {
go server()

time.Sleep(time.Millisecond)

// 1.解析string
fmt.Printf("\n\n=========1. bind string=====\n")
s := ""
err := gout.GET(":8080/rsp/body/string").
Debug(gout.DebugColor()).
BindBody(&s).
Do()

if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("need(string) got(%s)\n", s)

// 2.解析[]byte
fmt.Printf("\n\n=========2. bind string=====\n")
var b []byte
err = gout.GET(":8080/rsp/body/bytes").
Debug(gout.DebugColor()).
BindBody(&b).
Do()

if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("need(bytes) got(%s)\n", b)

// 3.解析int
fmt.Printf("\n\n=========3. bind int=====\n")
i := 0
err = gout.GET(":8080/rsp/body/int").
Debug(gout.DebugColor()).
BindBody(&i).
Do()

if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("need(65535) got(%d)\n", i)
//BindBody支持的更多基础类型有int, int8, int16, int32, int64
//uint, uint8, uint16, uint32, uint64
//float32, float64
}

0 comments on commit 2814c35

Please sign in to comment.