当前位置:网站首页>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
}
边栏推荐
- Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
- 联通网管协议框图
- MySQL5.6到8.0的账号迁移
- 基于 R 语言的判别分析介绍与实践 LDA和QDA
- 2022年非一线IT行业就业前景?
- 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)
- ViewPager fragments of nested data blank page abnormal problem analysis
- Redis源码剖析之跳表(skiplist)
- ARM板卡增加路由功能
- glide工具类的简单封装
猜你喜欢
5G China unicom 直放站 网管协议 实时性要求
用场景定义硬件,英码科技破解“边缘计算”密码
乐东消防救援大队应邀为干部开展消防安全培训
RTSP协议讲解
ViewPager fragments of nested data blank page abnormal problem analysis
How to reduce the size of desktop icons after the computer is reinstalled
The new features of ABP 6.0.0 - rc. 1
5G China unicom AP:B SMS ASCII 转码要求
GIN Bind模式获取参数和表单验证
Explanation of RTSP protocol
随机推荐
glibc memory management model freeing C library memory cache
Flutter Getting Started and Advanced Tour (1) - Getting to Know Flutter
陈强教授《机器学习及R应用》课程 第十七章作业
Extract EventBus encapsulation to base class using annotations
联通网管协议框图
LeetCode 37.解数独
ERP不规范,同事两行泪 (转载非原创)
激光熔覆在农机修复强化中的应用及研究方向
2022年非一线IT行业就业前景?
glide工具类的简单封装
30行代码实现蚂蚁森林自动偷能量
NFS 特别注意权限的问题
The new features of ABP 6.0.0 - rc. 1
Intra-group reverse order adjustment of K nodes
工作任务统计
剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
陈强教授《机器学习及R应用》课程 第十六章作业
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)
Unicom network management protocol block diagram