当前位置:网站首页>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
}
边栏推荐
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
- CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
- 万物皆可柯里化的 Ramda.js
- Redis源码剖析之robj(redisObject)
- Flutter Getting Started and Advanced Tour (7) GestureDetector
- Rmarkdown教程
- ARM board adds routing function
- 农村区县域农业电商如何做?数字化转型如何进行?
- 30行代码实现蚂蚁森林自动偷能量
- 造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片
猜你喜欢

GIN文件上传与返回

ansible-cmdb friendly display ansible collects host information

5G 联通网管设计思路

Flutter entry and advanced tour (6) Layout Widget

Data Mining-05

Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation

GIN Bind模式获取参数和表单验证

Flutter Getting Started and Advanced Tour (3) Text Widgets

FPGA-近日工作总结

Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
随机推荐
5G 联通网管设计思路
ERP不规范,同事两行泪 (转载非原创)
Clock frequency and baud rate count for serial communication in FPGA
FPGA-近日工作总结
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
Intra-group reverse order adjustment of K nodes
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
LnReader编译
如何修改data work上jdbc驱动的版本
工作任务统计
ViewPager fragments of nested data blank page abnormal problem analysis
基于 R 语言的判别分析介绍与实践 LDA和QDA
Use RecyclerView to implement three-level collapsed list
Introduction to Flutter advanced trip Dialog&Toast (10)
FPGA中串口通信的时钟频率和波特率计数
ABP中的数据过滤器 (转载非原创)
Sandbox中的进程/线程相关-2
ctfshow七夕杯2022
2.微服务'黑话'集锦及Eureka注册中心相关概念