一、获取查询或提交参数

ctx.Query解析url参数

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
// 通过ctx.Query直接解析url中的参数
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

// 通过ctx.Query直接解析url中的参数
func url(engine *gin.Engine) {
engine.GET("/stu", func(ctx *gin.Context) {
// 解析url参数,未传递时为空
name := ctx.Query("name")
// 解析url参数,未传递时使用默认值"china"
addr := ctx.DefaultQuery("addr", "china")

// 以text方式返回数据
ctx.String(http.StatusOK, name+" addr is "+addr+"\n")

// 以json格式返回数据
ctx.JSONP(http.StatusOK, gin.H{
"name": name,
"addr": addr,
})
})
}

func main() {
engine := gin.Default()
url(engine)
engine.Run(":5656")
}

运行结果示例

1
2
3
$ curl -XGET http://127.0.0.1:5656/stu\?name=zhangsan&addr=beijing
zhangsan addr is beijing
{"addr":"china","name":"zhangsan"}

ctx.PostForm解析表单参数

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
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

// 通过ctx.PostForm解析post提交的表单参数
func urlFrom(engine *gin.Engine) {
engine.POST("/home", func(ctx *gin.Context) {
// 解析post提交表单参数name,未提交时为空
name := ctx.PostForm("name")
// 解析post提交表单参数addr,未提交时使用默认值"shanghai"
addr := ctx.DefaultPostForm("addr", "shanghai")

// 以text方式返回数据
ctx.String(http.StatusOK, name+" addr is "+addr+"\n")

// 以json格式返回数据
ctx.JSONP(http.StatusOK, gin.H{
"name": name,
"addr": addr,
})
})
}

func main() {
engine := gin.Default()
urlFrom(engine)
engine.Run(":5656")
}

运行结果示例

1
2
3
curl -XPOST -d 'name=zhangsan&addr=beijing' http://127.0.0.1:5656/home
zhangsan addr is beijing
{"addr":"beijing","name":"zhangsan"}

ctx.QueryArray解析数组参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

// 通过ctx.QueryArray解析数组参数
func urlArray(engine *gin.Engine) {
engine.GET("/", func(ctx *gin.Context) {
ctx.JSON(200, ctx.QueryArray("media"))
})
}

func main() {
engine := gin.Default()
urlArray(engine)
engine.Run(":5656")
}

运行结果示例

1
2
$ curl http://localhost:5656/?media=blog&media=wechat          
["blog","wechat"]

二、上传文件

ctx.FormFile上传单个文件

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
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

// 通过ctx.FormFile上传单个文件
func upload_file(engine *gin.Engine) {
engine.POST("/upload", func(ctx *gin.Context) {
file,err := ctx.FormFile("file")
if err != nil {
fmt.Println(err)
ctx.String(http.StatusInternalServerError,file.Filename+" upload file failed!!!")
}else {
ctx.SaveUploadedFile(file,"./upload_data/"+file.Filename)
ctx.String(200,file.Filename+" upload success。。")
}
})
}

func main() {
engine := gin.Default()
upload_file(engine)
engine.Run(":5656")
}

运行结果示例

1
2
3
4
$ curl -F "file=@pvc.yaml" http://127.0.0.1:5656/upload
pvc.yaml upload success。。

# 上传成功后去指定的程序./upload_data目录下查看上传的文件

ctx.MultipartForm上传多个文件

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
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)

// 通过上传多个文件
func upload_files(engine *gin.Engine) {
engine.POST("/uploads", func(ctx *gin.Context) {
form, _ := ctx.MultipartForm()
files := form.File["files"]
for _, file := range files {
ctx.SaveUploadedFile(file, "./upload_data/"+file.Filename)
}
ctx.String(http.StatusOK, strconv.Itoa(len(files))+" file upload success...")
})
}

func main() {
engine := gin.Default()
upload_files(engine)
engine.Run(":5656")
}

运行结果示例

1
2
3
4
$ curl -F "files=@pvc.yaml" -F "files=@sunlogin_helper.log" http://127.0.0.1:5656/uploads
2 file upload success...

# 上传成功后去指定的程序./upload_data目录下查看上传的文件