Gin 使用示例(三十九):HTTP 方法


示例代码:

func getting(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP GET Method")
}
    
func posting(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP POST Method")
}
    
func putting(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP PUT Method")
}
    
func deleting(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP DELETE Method")
}
    
func patching(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP PATCH Method")
}
    
func head(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP HEAD Method")
}
    
func options(c *gin.Context)  {
    c.String(http.StatusOK, "HTTP OPTIONS Method")
}
    
func main() {
    // Creates a gin router with default middleware:
    // logger and recovery (crash-free) middleware
    router := gin.Default()
    
    router.GET("/get", getting)
    router.POST("/post", posting)
    router.PUT("/put", putting)
    router.DELETE("/delete", deleting)
    router.PATCH("/patch", patching)
    router.HEAD("/head", head)
    router.OPTIONS("/options", options)
    
    // By default it serves on :8080 unless a
    // PORT environment variable was defined.
    router.Run(":8088")
}

运行上述代码启动服务器:

-w872

在终端窗口通过 curl 命令模拟请求进行测试:

-w622


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Gin 使用示例(三十八):使用 BasicAuth 中间件

>> 下一篇: Gin 使用示例(四十):使用中间件