Go语言中Get/Post请求测试实例分析(get,go语言,post,开发技术)

时间:2024-05-02 13:12:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

gin安装

先将gin安装一下,安装依赖go语言还是比较方便的。

在安装之前先配置一下goproxy。

命令如下:

goenv-wGO111MODULE=ongoenv-wGOPROXY=https://mirrors.aliyun.com/goproxy///阿里代理goenv-wGOPROXY=https://goproxy.cn//七牛云代理

安装一下gin,命令如下:

gogetgithub.com/gin-gonic/gin

Get请求测试

实现一个web服务还是比较简单的,创建一个router,绑定路由规则即可。先测试几个Get请求。

样例代码如下:

packagemainimport( "github.com/gin-gonic/gin" "net/http")funcmain(){ router:=gin.Default() router.GET("/",func(context*gin.Context){ context.String(http.StatusOK,"helloworld") }) router.GET("/test/:name",func(context*gin.Context){ name:=context.Param("name") context.String(http.StatusOK,"checkparam%s",name) }) router.GET("/test1",func(context*gin.Context){ name:=context.DefaultQuery("name","张三") gender:=context.Query("gender") context.String(http.StatusOK,"他叫%s,性别:%s",name,gender) }) router.Run(":8080")}

执行结果

[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 / --> main.main.func1 (3 handlers)
[GIN-debug] GET /test/:name --> main.main.func2 (3 handlers)
[GIN-debug] GET /test1 --> main.main.func3 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080

[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend yo
u to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-
proxies for details.
[GIN-debug] Listening and serving HTTP on :8080

测试一下,这里我是用的接口测试工具为ApiPost

Go语言中Get/Post请求测试实例分析

Go语言中Get/Post请求测试实例分析

Go语言中Get/Post请求测试实例分析

注意

1、在使用context.DefaultQuery方法的时候,可以提供一个默认值。

2、除了可以使用":"来获取路径参数外,可以使用"*",可以匹配更多规则。我个人感觉我不会这么用get请求参数。

Post请求测试

Post请求是在项目中使用的比较多的,而且不管是使用form获取参数还是body,都十分常见。

同时返回的数据也不可能使用一行字符串,实际项目中还是使用json格式居多。

所以下面我使用form参数和body参数实现了一下post测试接口。

完成代码如下

packagemainimport( "encoding/json" "fmt" "github.com/gin-gonic/gin" "io/ioutil" "net/http")typeResultstruct{ Namestring`json:"name"` Ageint`json:"age"`}//反序列化为结构体对象funcparseJson(astring)Result{ fmt.Printf("原始字符串:%s\n",a) varcResult iferr:=json.Unmarshal([]byte(a),&c);err!=nil{ fmt.Println("Error=",err) returnc } returnc}funcmain(){ router:=gin.Default() router.GET("/",func(context*gin.Context){ context.String(http.StatusOK,"helloworld") }) router.GET("/test/:name",func(context*gin.Context){ name:=context.Param("name") context.String(http.StatusOK,"checkparam%s",name) }) router.GET("/test1",func(context*gin.Context){ name:=context.DefaultQuery("name","张三") gender:=context.Query("gender") context.String(http.StatusOK,"他叫%s,性别:%s",name,gender) }) router.POST("/testPost",func(context*gin.Context){ name:=context.PostForm("name") nick:=context.DefaultPostForm("nick","leo") context.JSON(http.StatusOK,gin.H{ "status":gin.H{ "code":http.StatusOK, "success":true, }, "name":name, "nick":nick, }) }) router.POST("/testPost2",func(context*gin.Context){ data,_:=ioutil.ReadAll(context.Request.Body) fmt.Println(string(data)) context.JSON(http.StatusOK,gin.H{ "code":http.StatusOK, "data":parseJson(string(data)), }) }) router.Run(":8080")}

测试一下testPost和testPost2接口

Go语言中Get/Post请求测试实例分析

Go语言中Get/Post请求测试实例分析

注意

1、使用context.DefaultPostForm方法可以提供一个默认值。

2、可以使用gin.H方法构造json结构返回。

3、将获得打参数反序列化为结构体,这部分的代码使用到之前讲json解析的笔记。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Go语言中Get/Post请求测试实例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:cypress如何测试本地web应用下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18