Gin请求动态url-context.Param
- 定义
path
:path:/admin/:xxx
- 获取参数
c.Param("xxx")
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/back/:name/:action/to", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
c.JSON(http.StatusOK, gin.H{
"name": name,
"action": action,
})
})
r.Run(":8888")
}
=> curl 127.0.0.1:8888/back/cunwang/add/to
<= {"action":"add","name":"cunwang"}%
Gin请求动态url-context.ShoudBindUri
ShoudBindUri
可以让我们对动态参数进行一些定义约束通过接收一个结构体实例的指针,结构体定义的时候通过
tag
做一些字段约束,那么就可以达到传值约束的效果
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type IdAction struct {
Id string `uri:"id" binding:"required,uuid"` //其实这个required在uri做不到什么校验,不传直接404了~
Action string `uri:"action" binding:"required"`
}
func main() {
r := gin.Default()
r.GET("/back/:id/:action", func(c *gin.Context) {
var idAction IdAction
err := c.ShouldBindUri(&idAction)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"id": idAction.Id,
"action": idAction.Action,
})
})
r.Run(":8888")
}
# 校验通过
~ curl 127.0.0.1:8888/back/8cb82a68-bde5-7029-ad39-917f684ffb07/addclea
{"action":"addclea","name":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
# 校验不通过
~ curl 127.0.0.1:8888/back/11111122/add
{"err":"Key: 'NameAction.Name' Error:Field validation for 'Name' failed on the 'uuid' tag"}%
Gin 获取Get
请求参数 context.Query
r.GET("/back", func(c *gin.Context) {
id := c.Query("id")
action := c.Query("action")
c.JSON(http.StatusOK, gin.H{
"id": id,
"action": action,
})
})
~ curl 127.0.0.1:8888/back?id=1&action=add
{"action": "add","id": "1"}
Gin 获取Get
请求参数的默认值context.DefaultQuery
r.GET("/back", func(c *gin.Context) {
id := c.DefaultQuery("id", "1")
action := c.DefaultQuery("action", "add")
c.JSON(http.StatusOK, gin.H{
"id": id,
"action": action,
})
})
# 无参数携带,使用默认值
~ curl http://127.0.0.1:8888/back
{"action":"add","id":"1"}%
# 有参数携带 使用参数值
~ curl http://127.0.0.1:8888/back\?id\=2\&action\=delete
{"action":"delete","id":"2"}%
Gin 获取Get
请求参数context.ShouldBind
c.ShouldBind
,配合form tag
可以完成 Get的表单验证
type IdAction struct {
Id string `form:"id" binding:"required,uuid"`
Action string `form:"action" binding:"required"`
}
r.GET("/getShouldBind", func(c *gin.Context) {
var idAction IdAction
err := c.ShouldBind(&idAction)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"err": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"id": idAction.Id,
"action": idAction.Action,
})
})
# 校验失败!
~ curl http://127.0.0.1:8888/getShouldBind\?id\=1\&action\=delete
{"err":"Key: 'IdAction.Id' Error:Field validation for 'Id' failed on the 'uuid' tag"}%
# 校验成功!
~ curl http://127.0.0.1:8888/getShouldBind\?id\=8cb82a68-bde5-7029-ad39-917f684ffb07\&action\=delete
{"action":"delete","id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
Get使用
**shouldBind**
可以接受拼接到**url**
的**Params**
参数,也可以接受**contentType: form-data**
的参数
r.GET("/getShouldBind", func(c *gin.Context) {
var idAction IdAction
// id := c.DefaultQuery("id", "1")
// action := c.DefaultQuery("action", "add")
err := c.ShouldBind(&idAction)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"err": err.Error(),
"content-type": c.Request.Header["Content-Type"],
})
return
}
c.JSON(http.StatusOK, gin.H{
"id": idAction.Id,
"action": idAction.Action,
"content-type": c.Request.Header,
})
})
# form-data
curl --location --request GET 'http://localhost:8888/getShouldBind' --form 'id="8cb82a68-bde5-7029-ad39-917f684ffb07"' --form 'action="add"'
{"action":"add","content-type":["multipart/form-data; boundary=------------------------0f2bd960b85dda6b"],"id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
# params拼接url
curl --location --request GET 'http://localhost:8888/getShouldBind?id=8cb82a68-bde5-7029-ad39-917f684ffb07&action=add'
{"action":"add","content-type":null,"id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
Gin 获取Post
请求参数 context.PostForm
r.POST("/post", func(c *gin.Context) {
id := c.PostForm("id")
action := c.PostForm("action")
c.JSON(http.StatusOK, gin.H{
"id": id,
"action": action,
})
})
~ curl http://127.0.0.1:8888/post -d id=2 -d action=delete
{"action":"delete","id":"2"}%
Gin 获取Post
请求参数的默认值context.DefaultPostForm
r.POST("/post", func(c *gin.Context) {
id := c.DefaultPostForm("id", "1")
action := c.DefaultPostForm("action", "add")
c.JSON(http.StatusOK, gin.H{
"id": id,
"action": action,
})
})
# 参数完整 使用参数值
~ curl http://127.0.0.1:8888/post -d id=2 -d action=update
{"action":"update","id":"2"}%
# 无参数 使用默认值 缺少action
~ curl http://127.0.0.1:8888/post -d id=1
{"action":"add","id":"1"}%
Gin 获取Post
请求参数context.ShouldBind
c.ShouldBind
,配合form tag
可以完成Post
的表单验证
r.POST("/postShouldBind", func(c *gin.Context) {
var idAction IdAction
err := c.ShouldBind(&idAction)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"err": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"id": idAction.Id,
"action": idAction.Action,
})
})
# post shouldBind校验不通过
~ curl http://127.0.0.1:8888/postShouldBind -d id=1 -d action=add
{"err":"Key: 'IdAction.Id' Error:Field validation for 'Id' failed on the 'uuid' tag"}%
# post shouldBind校验通过
~ curl http://127.0.0.1:8888/postShouldBind -d id=8cb82a68-bde5-7029-ad39-917f684ffb07 -d action=add
{"action":"add","id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
Post请求使用shouldBind 可以接收
ContentType
为以下类型的
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
form-data
curl --location --request POST 'http://localhost:8888/postShouldBind' \
--header 'Content-Type: application/json' \
--data-raw '{
"id":"8cb82a68-bde5-7029-ad39-917f684ffb07",
"action":"add"
}'
{"action":"add","content-type":["application/json"],"id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
curl --location --request POST 'http://localhost:8888/postShouldBind' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'id=8cb82a68-bde5-7029-ad39-917f684ffb07' \
--data-urlencode 'action=add'
{"action":"add","content-type":["application/x-www-form-urlencoded"],"id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
curl --location --request POST 'http://localhost:8888/postShouldBind' \
--form 'id="8cb82a68-bde5-7029-ad39-917f684ffb07"' \
--form 'action="add"'
{"action":"add","content-type":["multipart/form-data; boundary=------------------------32f115dc003b4c6c"],"id":"8cb82a68-bde5-7029-ad39-917f684ffb07"}%
Gin 获取Post
请求参数context.ShouldBindJSON
这个其实和上面的用法差不多,只不过ta只能接收
Content-Type
为application/json
类型的数据
r.POST("/postShouldBindJson", func(c *gin.Context) {
fmt.Print(c.Request.Header)
var idAction IdAction
err := c.ShouldBindJSON(&idAction)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"err": err.Error(),
"content-type": c.Request.Header["Content-Type"],
})
return
}
c.JSON(http.StatusOK, gin.H{
"id": idAction.Id,
"action": idAction.Action,
"content-type": c.Request.Header["Content-Type"],
})
})
Content-Type: application/x-www-form-urlencoded
无法解析
```bash curl —location —request POST ‘http://localhost:8888/postShouldBindJson‘ \ —header ‘Content-Type: application/x-www-form-urlencoded’ \ —data-urlencode ‘id=8cb82a68-bde5-7029-ad39-917f684ffb07’ \ —data-urlencode ‘action=add’
{“content-type”:[“application/x-www-form-urlencoded”],”err”:”invalid character ‘i’ looking for beginning of value”}%
- `form-data` 无法解析
```bash
curl --location --request POST 'http://localhost:8888/postShouldBindJson' \
--form 'id="8cb82a68-bde5-7029-ad39-917f684ffb07"' \
--form 'action="add"'
{"content-type":["multipart/form-data; boundary=------------------------a596ddaeeaf65568"],"err":"invalid character '-' in numeric literal"}%
Content-Type: application/json
可以解析
```bash curl —location —request POST ‘http://localhost:8888/postShouldBindJson‘ \ —header ‘Content-Type: application/json’ \ —data-raw ‘{ “id”:”8cb82a68-bde5-7029-ad39-917f684ffb07”, “action”:”add” }’
{“action”:”add”,”content-type”:[“application/json”],”id”:”8cb82a68-bde5-7029-ad39-917f684ffb07”}% ```
其实不同的请求方式,获取参数的方式都差不多更多,还说慢慢使用积累