企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Gin路由 === ### Get 获取get参数 ` name := ctx.Query("name") ` ``` func main() { app := gin.Default() app.GET("/", func(ctx *gin.Context) { ctx.JSON(200,gin.H{ "message":"Hello World", }) }) app.GET("/pps",pps) // /pps?name=?&password=? app.Run(":8085") } func pps(ctx *gin.Context) { name := ctx.Query("name") pwd := ctx.Query("password") ctx.JSON(200,gin.H{ "name":name, "pwd":pwd, }) } ``` ### 路由参数 ``` app.GET("/name/:name/:age",hello) func hello(ctx *gin.Context) { name := ctx.Param("name") age := ctx.Param("age") ctx.JSON(200,gin.H{ "name":name, "age":age, }) ``` ### POST ``` func ppos(ctx *gin.Context) { name := ctx.PostForm("name") password := ctx.PostForm("password") ctx.JSON(200,gin.H{ "name":name, "password":password, }) } ``` ### JSON ~~~ func jsson(ctx *gin.Context) { body := ctx.Request.Body defer body.Close() type User struct { User string `json:"user"` Password string `json:password` } bytes, _ := ioutil.ReadAll(body) user := &User{} json.Unmarshal(bytes,user) ctx.JSON(200,user) } ~~~ ### JSON参数绑定 注意阿,要设置type:application/json才能接受到! ~~~ func jsBan(ctx *gin.Context) { type user struct { User string `json:"user"` Password string `json:"password"` } u := &user{} err := ctx.Bind(u) if err != nil { fmt.Println(err.Error()) } fmt.Println(u.User) fmt.Println(u.Password) ctx.JSON(http.StatusOK,u) } ~~~