当前位置:网站首页>GET POST PUT DELETE request in GIN
GET POST PUT DELETE request in GIN
2022-08-09 13:48:00 【One Leaf Knows Autumn @qqy】
前言
感谢开源项目gin-vue-admin,以及1010工作室的教程,项目文档
我只是在跟着学习,然后记录下笔记而已,可能会有新的代码加入,但是本质还是跟着学习的一个过程.
修改端口号
Look again at the example program written in the previous article
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default() //启动gin路由,携带基础中间件启动
//让启动的路由接收get请求,且是/ping,运行一个匿名函数,将gin的上下文传入
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
//将接收的信息返 回
"message": "pong",
})
})
r.Run()
}
It can be found that the last is byRun()The function runs the service,所以在执行Run()时,可以更改端口号,For example here if the port number is used1010的话,只需要将1010传入Run()函数中.
r.Run(":1010")
At this point re-run the program,再用postman去进行请求,It can be found that the correct result is still obtained,The description has passed1010interface to access data.
At this point, all requests are defined in the code:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default() //启动gin路由,携带基础中间件启动
r.GET("/path/:id", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.POST("/path", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.DELETE("/path/:id", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.PUT("/path", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run(":1010") // listen and serve on 0.0.0.0:8080
}
GET
getThe request can see the parameters carried in the URL,That is, the parameter isurl和uri中进行挂载.
URL:Uniform Resource Locator 统一资源定位符;
URI:Uniform Resource Identifier 统一资源标识符;
URL: 关键词-“资源定位”, That is by writing the path,Indicates the location of the resource,location is specified,Usually resources are also specified,例如302班1列3排的同学,Identity can be determined.
URI: 关键词-“资源标识”, A male classmate named Xiao Ming.We know for sure,有这么个人,性别男,名小明.
修改代码
然后再将GETunder the modification,
r.GET("/path/:id", func(c *gin.Context) {
//Take out the parameters in the request
id := c.Param("id")
user := c.Query("user")
pwd := c.Query("pwd")
c.JSON(200, gin.H{
"id": id,
"user": user,
"pwd": pwd,
})
})
这里利用ParamPut the request in the address barid(这里是123提取出来),和用Querywill follow the parametersuser和pwd提取出来,Then return these three parameters
可以看见,postmanThe request initiated in has already received the correct request and returned.
Here if no username is given in the request orpwd,Then the server cannot resolve the corresponding parameters,So the return will also be blank.
其中Queryis the read parameter,Leave empty when this parameter is not present,DefaultQueryis the read parameter,Gives a specified default value when this parameter is not present.
POST
参数在form中 、body中,或者在uri中
formIt is provided to the backend in the form of a form
body是常用的json进行交互
将post部分重写
r.POST("/path", func(c *gin.Context) {
user := c.DefaultPostForm("user", "qqyking")
pwd := c.PostForm("pwd")
c.JSON(200, gin.H{
"user": user,
"pwd": pwd,
})
})
在postman中构建post请求
Also tried at the beginningurl中带参数,然后使用post请求访问,但是都失败了.
其中PostFormis the read parameter,Leave empty when this parameter is not present,DefaultPostFormis the read parameter,Gives a specified default value when this parameter is not present.
DELETE
一般情况为uri,The same can bebody
用法和GET基本相同,But the difference is that it is also available from bodyInternal parameters
然后重写DELETE
r.DELETE("/path/:id", func(c *gin.Context) {
id := c.Param("id")
c.JSON(200, gin.H{
"id": id,
})
})
然后在postman中构建DELETE请求
PUT
参数在form body或者uri中
将put部分重写
r.PUT("/path", func(c *gin.Context) {
user := c.DefaultPostForm("user", "qqyking")
pwd := c.PostForm("pwd")
c.JSON(200, gin.H{
"user": user,
"pwd": pwd,
})
})
在postman中构建put请求
可以看出POST和PUTUsage is exactly the same,Only the way of request is different
So passGINThe interface implements CRUD
Final full code example
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
r.GET("/path/:id", func(c *gin.Context) {
//Take out the parameters in the request
id := c.Param("id")
user := c.Query("user")
pwd := c.DefaultQuery("pwd", "123456")
c.JSON(200, gin.H{
"id": id,
"user": user,
"pwd": pwd,
})
})
r.POST("/path", func(c *gin.Context) {
user := c.DefaultPostForm("user", "qqyking")
pwd := c.PostForm("pwd")
c.JSON(200, gin.H{
"user": user,
"pwd": pwd,
})
})
r.DELETE("/path/:id", func(c *gin.Context) {
id := c.Param("id")
c.JSON(200, gin.H{
"id": id,
})
})
r.PUT("/path", func(c *gin.Context) {
user := c.DefaultPostForm("user", "qqyking")
pwd := c.PostForm("pwd")
c.JSON(200, gin.H{
"user": user,
"pwd": pwd,
})
})
r.Run(":1010") // listen and serve on 0.0.0.0:8080
}
边栏推荐
- Flutter entry and advanced tour (6) Layout Widget
- 生成上传密钥和密钥库
- Introduction to Flutter advanced trip Dialog&Toast (10)
- telnet+ftp to control and upgrade the device
- Yocto 可以下载的第三方库
- 卷积神经网络表征可视化研究综述(1)
- FPGA-在ISE中错误总结(更新中)
- Clock frequency and baud rate count for serial communication in FPGA
- MySQL备份与恢复 (转载非原创)
- Use RecyclerView to implement three-level collapsed list
猜你喜欢
How to reduce the size of desktop icons after the computer is reinstalled
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
第六届“强网杯”全国网络安全挑战赛
jenkins api创建自定义pipeline
从NPU-SLAM-EDA技术分析
Flutter Getting Started and Advanced Tour (8) Button Widget
Flutter Getting Started and Advanced Tour (4) Text Input Widget TextField
RTSP协议讲解
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
随机推荐
乐东消防救援大队应邀为干部开展消防安全培训
Flutter entry and advanced tour (6) Layout Widget
ABP中的数据过滤器 (转载非原创)
流量焦虑背后是企业对客户关系管理的不足
Flutter Getting Started and Advanced Tour (1) - Getting to Know Flutter
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
telnet+ftp to control and upgrade the device
联通网管协议框图
Unicom network management protocol block diagram
Rmarkdown教程
激光熔覆在农机修复强化中的应用及研究方向
陈强教授《机器学习及R应用》课程 第十四章作业
gin的中间件和路由分组
MySQL备份与恢复 (转载非原创)
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
Use RecyclerView to implement three-level collapsed list
造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片
ftplib+ tqdm 上传下载进度条
基于 R 语言的判别分析介绍与实践 LDA和QDA