mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
绑定form-data
This commit is contained in:
parent
73da7abdaa
commit
c80c9c48f7
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type StructA struct {
|
||||
FieldA string `form:"field_a"`
|
||||
}
|
||||
|
||||
type StructB struct {
|
||||
NestedStruct StructA
|
||||
FieldB string `form:"field_b"`
|
||||
}
|
||||
|
||||
type StructC struct {
|
||||
NestedStructPointer *StructA
|
||||
FieldC string `form:"field_c"`
|
||||
}
|
||||
|
||||
type StructD struct {
|
||||
NestedAnonyStruct struct {
|
||||
FieldX string `form:"field_x"`
|
||||
}
|
||||
FieldD string `form:"field_d"`
|
||||
}
|
||||
|
||||
func GetDataB(c *gin.Context) {
|
||||
var b StructB
|
||||
c.Bind(&b)
|
||||
c.JSON(200, gin.H{
|
||||
"a": b.NestedStruct,
|
||||
"b": b.FieldB,
|
||||
})
|
||||
}
|
||||
|
||||
func GetDataC(c *gin.Context) {
|
||||
var b StructC
|
||||
c.Bind(&b)
|
||||
c.JSON(200, gin.H{
|
||||
"a": b.NestedStructPointer,
|
||||
"c": b.FieldC,
|
||||
})
|
||||
}
|
||||
|
||||
func GetDataD(c *gin.Context) {
|
||||
var b StructD
|
||||
c.Bind(&b)
|
||||
c.JSON(200, gin.H{
|
||||
"x": b.NestedAnonyStruct,
|
||||
"d": b.FieldD,
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.GET("/getb", GetDataB)
|
||||
router.GET("/getc", GetDataC)
|
||||
router.GET("/getd", GetDataD)
|
||||
router.Run()
|
||||
}
|
||||
|
||||
/**
|
||||
$ curl "http://localhost:8080/getb?field_a=hello&field_b=world"
|
||||
{"a":{"FieldA":"hello"},"b":"world"}
|
||||
$ curl "http://localhost:8080/getc?field_a=hello&field_c=world"
|
||||
{"a":{"FieldA":"hello"},"c":"world"}
|
||||
$ curl "http://localhost:8080/getd?field_x=hello&field_d=world"
|
||||
{"d":"world","x":{"FieldX":"hello"}}
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user