[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in"debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /get --> go-use/practise.UseHttp.func1 (3 handlers) [GIN-debug] POST /post --> go-use/practise.UseHttp.func2 (3 handlers) [GIN-debug] PUT /put --> go-use/practise.UseHttp.func3 (3 handlers) [GIN-debug] DELETE /del --> go-use/practise.UseHttp.func4 (3 handlers) [GIN-debug] HEAD /head --> go-use/practise.UseHttp.func5 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080
调用示例:
# GET请求 ➜ ~ curl -X GET http://127.0.0.1:8080/get {"method":"get","msg":"请求成功"}% # 使用GET请求接收post的路由会报错 ➜ ~ curl -X GET http://127.0.0.1:8080/post 404 page not found% # POST请求 ➜ ~ curl -X POST http://127.0.0.1:8080/post {"method":"post","msg":"请求成功"}% # PUT 请求 ➜ ~ curl -X PUT http://127.0.0.1:8080/put {"method":"put","msg":"请求成功"}% # DELETE请求 ➜ ~ curl -X DELETE http://127.0.0.1:8080/del {"method":"del","msg":"请求成功"}% # HEAD请求 ➜ ~ curl -i -X HEAD http://127.0.0.1:8080/head Warning: Setting custom HTTP method to HEAD with -X/--request may not work the Warning: way you want. Consider using -I/--head instead. HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Mon, 26 Apr 2021 08:55:19 GMT Content-Length: 38
需要注意的是:当路由注册成指定HTTP方法时,必须以同样的方式请求,否则会返回:404 page not found
2.2 匹配所有HTTP请求
使用Any方法,可以匹配所有的HTTP的方法(GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE)