GET, POST, PUT, PATCH, DELETE, OPTIONS

  1. func testfunc(ctx *gin.Context) {
  2. ctx.JSON(http.StatusOK,gin.H{
  3. "ping":"pong",
  4. })
  5. }
  6. func main() {
  7. // 禁用控制台颜色
  8. // gin.DisableConsoleColor()
  9. // 使用默认中间件创建一个 gin 路由:
  10. // 日志与恢复中间件。
  11. router := gin.Default()
  12. router.GET("/someGet", testfunc)
  13. router.POST("/somePost", testfunc)
  14. router.PUT("/somePut", testfunc)
  15. router.DELETE("/someDelete", testfunc)
  16. router.PATCH("/somePatch", testfunc)
  17. router.HEAD("/someHead", testfunc)
  18. router.OPTIONS("/someOptions", testfunc)
  19. // 默认情况下,它使用:8080,除非定义了 PORT 环境变量。
  20. router.Run()
  21. // router.Run(":3000") 硬编码端口
  22. }

path中的参数

  1. func main() {
  2. router := gin.Default()
  3. // 这个处理器可以匹配 /user/john , 但是它不会匹配 /user
  4. router.GET("/user/:name", func(c *gin.Context) {
  5. name := c.Param("name")
  6. c.String(http.StatusOK, "Hello %s", name)
  7. })
  8. // 但是,这个可以匹配 /user/john 和 /user/john/send
  9. // 如果没有其他的路由匹配 /user/john , 它将重定向到 /user/john/
  10. router.GET("/user/:name/*action", func(c *gin.Context) {
  11. name := c.Param("name")
  12. action := c.Param("action")
  13. message := name + " is " + action
  14. c.String(http.StatusOK, message)
  15. })
  16. router.Run(":8080")
  17. }

查询字符串参数

  1. func main() {
  2. router := gin.Default()
  3. // 请求响应匹配的 URL: /welcome?firstname=Jane&lastname=Doe
  4. router.GET("/welcome", func(c *gin.Context) {
  5. firstname := c.DefaultQuery("firstname", "Guest")
  6. // 这个是 c.Request.URL.Query().Get("lastname") 的快捷方式。
  7. lastname := c.Query("lastname")
  8. c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
  9. })
  10. router.Run(":8080")
  11. }

Multipart/Urlencoded 表单

  1. func main() {
  2. router := gin.Default()
  3. router.POST("/form_post", func(c *gin.Context) {
  4. message := c.PostForm("message")
  5. nick := c.DefaultPostForm("nick", "anonymous")
  6. c.JSON(200, gin.H{
  7. "status": "posted",
  8. "message": message,
  9. "nick": nick,
  10. })
  11. })
  12. router.Run(":8080")
  13. }

查询字符串参数 + post 表单

  1. POST /post?id=1234&page=1 HTTP/1.1
  2. Content-Type: application/x-www-form-urlencoded
  3. name=manu&message=this_is_great
  1. func main() {
  2. router := gin.Default()
  3. router.POST("/post", func(c *gin.Context) {
  4. id := c.Query("id")
  5. page := c.DefaultQuery("page", "0")
  6. name := c.PostForm("name")
  7. message := c.PostForm("message")
  8. fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)
  9. })
  10. router.Run(":8080")
  11. }
  1. id: 1234; page: 1; name: manu; message: this_is_great

上传文件

单文件

  1. func main() {
  2. router := gin.Default()
  3. // 为 multipart 表单设置一个较低的内存限制(默认是 32 MiB)
  4. // router.MaxMultipartMemory = 8 << 20 // 8 MiB
  5. router.POST("/upload", func(c *gin.Context) {
  6. file, _ := c.FormFile("file")
  7. log.Println(file.Filename)
  8. // 上传文件到指定的 dst 。
  9. // c.SaveUploadedFile(file, dst)
  10. c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
  11. })
  12. router.Run(":8080")
  13. }

多文件

  1. func main() {
  2. router := gin.Default()
  3. // 为 multipart 表单设置一个较低的内存限制(默认是 32 MiB)
  4. // router.MaxMultipartMemory = 8 << 20 // 8 MiB
  5. router.POST("/upload", func(c *gin.Context) {
  6. // Multipart form
  7. form, _ := c.MultipartForm()
  8. files := form.File["upload[]"]
  9. for _, file := range files {
  10. log.Println(file.Filename)
  11. // 上传文件到指定的 dst.
  12. // c.SaveUploadedFile(file, dst)
  13. }
  14. c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
  15. })
  16. router.Run(":8080")
  17. }