Skip to content

Commit

Permalink
update #82 新增请求header demo
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Nov 9, 2019
1 parent 9920697 commit 8012605
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
6 changes: 3 additions & 3 deletions _example/query.go
Expand Up @@ -23,7 +23,7 @@ func server() {
router := gin.New()
router.GET("/test.query", func(c *gin.Context) {
q2 := testQuery{}
err := c.BindQuery(&q2)
err := c.ShouldBindQuery(&q2)
if err != nil {
c.String(500, "fail")
return
Expand Down Expand Up @@ -74,7 +74,7 @@ func main() {

// 3.使用结构体
// 使用结构体需要设置query tag
fmt.Printf("======2. SetQuery======use struct=====\n")
fmt.Printf("======3. SetQuery======use struct=====\n")
err = gout.GET(":8080/test.query").
Debug(gout.DebugColor()).
SetQuery(testQuery{Q1: "v1",
Expand All @@ -91,7 +91,7 @@ func main() {
}

// 4.使用string
fmt.Printf("======2. SetQuery======use string=====\n")
fmt.Printf("======4. SetQuery======use string=====\n")
err = gout.GET(":8080/test.query").
Debug(gout.DebugColor()).
SetQuery("q1=v1&q2=2&q3=3.14&q4=3.1415&q5=1564295760&q6=1564295760000001000&q7=2019-07-28").
Expand Down
90 changes: 90 additions & 0 deletions _example/req_header.go
@@ -0,0 +1,90 @@
package main

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

type testHeader struct {
H1 string `header:"h1"`
H2 int `header:"h2"`
H3 float32 `header:"h3"`
H4 float64 `header:"h4"`
H5 time.Time `header:"h5" time_format:"unix"`
H6 time.Time `header:"h6" time_format:"unixNano"`
H7 time.Time `header:"h7" time_format:"2006-01-02"`
}

func server() {
router := gin.New()
router.GET("/test.header", func(c *gin.Context) {
h2 := testHeader{}
err := c.BindHeader(&h2)
if err != nil {
c.String(500, "fail")
return
}
})

router.Run()
}

func main() {
go server()

time.Sleep(time.Millisecond)
// 1.使用gout.H
fmt.Printf("======1. SetHeader======use gout.H=====\n")
err := gout.GET(":8080/test.header").
Debug(gout.DebugColor()).
SetHeader(gout.H{"h1": "v1",
"h2": 2,
"h3": float32(3.14),
"h4": 4.56,
"h5": time.Now().Unix(),
"h6": time.Now().UnixNano(),
"h7": time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}

// 2.使用数组变量
fmt.Printf("======2. SetHeader======use array=====\n")
err = gout.GET(":8080/test.header").
Debug(gout.DebugColor()).
SetHeader(gout.A{"h1", "v1",
"h2", 2,
"h3", float32(3.14),
"h4", 4.56,
"h5", time.Now().Unix(),
"h6", time.Now().UnixNano(),
"h7", time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}

// 3.使用结构体
// 使用结构体需要设置"header" tag
fmt.Printf("======3. SetHeader======use struct=====\n")
err = gout.GET(":8080/test.header").
Debug(gout.DebugColor()).
SetHeader(testHeader{H1: "v1",
H2: 2,
H3: float32(3.14),
H4: 4.56,
H5: time.Now(),
H6: time.Now(),
H7: time.Now()}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}

}

0 comments on commit 8012605

Please sign in to comment.