当前位置:网站首页>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
}
原网站

版权声明
本文为[One Leaf Knows Autumn @qqy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244282267.html