当前位置:网站首页>[go practical basis] how to verify request parameters in gin
[go practical basis] how to verify request parameters in gin
2022-07-02 09:08:00 【Novice practice】
Catalog
3、 ... and 、 Rookie actual combat
(1)GET Method , Add routing parameters and callbacks outside the verification range
(2)GET Method , Add the route parameters and callbacks outside the verification range again
(3)GET Method , Add routing parameters and callbacks within the validation range
One 、 brief introduction
Actual combat scene : How to use gin Verify request parameters
Two 、 Knowledge point
gin route
Structure
Structure parameter binding
Parameter validation
http Status code
3、 ... and 、 Rookie actual combat
Make arrangements now !
1、 establish go file
/*
* @Author: Rookie actual combat
* @Description: gin How to verify request parameters
*/
// Knowledge point :
// # gin route
// # Structure
// # Structure parameter binding
// # Parameter validation
// # http Status code
package main
// Import package
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"runtime"
)
// Defining structure
type Person struct {
Name string `form:"name" binding:"required"`
Age int `form:"age" binding:"required,gt=10"`
}
// The main function
func main() {
// Print using built-in functions
println("Hello", " Rookie actual combat ")
println(" Actual combat scene : ", "gin How to verify request parameters ")
// initialization
r := gin.Default()
// GET Method , Add routing parameters and callbacks
r.GET("/bind", testBind)
r.POST("/bind", testBind)
// Use package functions to print
fmt.Printf(" edition : %s \n", runtime.Version())
// Start the framework program , Default 8080 port
r.Run()
}
// Automatically parse data according to the structure definition
func testBind(c *gin.Context) {
var person Person
// Here is according to the request header Of content-type To do it binding operation
if err := c.ShouldBind(&person); err != nil {
c.String(http.StatusInternalServerError, "person bind error %v \n", err)
} else {
c.String(http.StatusOK, "%v \n", person)
}
}
// curl Verification mode
// bind get
// curl -X GET "http://127.0.0.1:8080/bind?a=1"
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty&age=5"
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty&age=21"
2、 Running results
Hello Rookie actual combat
Actual combat scene : gin How to verify request parameters
[GIN-debug] Listening and serving HTTP on :8080
(1)GET Method , Add routing parameters and callbacks outside the verification range

(2)GET Method , Add the route parameters and callbacks outside the verification range again

(3)GET Method , Add routing parameters and callbacks within the validation range

Rookie actual combat , Continuous learning !
边栏推荐
- 队列的基本概念介绍以及典型应用示例
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- C4D quick start tutorial - C4d mapping
- Image transformation, transpose
- 【Go实战基础】gin 如何验证请求参数
- Essay: RGB image color separation (with code)
- 选择排序和插入排序
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- Don't spend money, spend an hour to build your own blog website
- Leetcode sword finger offer brush questions - day 23
猜你喜欢

队列管理器running状态下无法查看通道

Matplotlib剑客行——初相识Matplotlib

Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings

I've taken it. MySQL table 500W rows, but someone doesn't partition it?

Solution and analysis of Hanoi Tower problem

Function ‘ngram‘ is not defined

C language - Blue Bridge Cup - 7 segment code

Qt——如何在QWidget中设置阴影效果

kubernetes部署loki日志系统

Linux binary installation Oracle database 19C
随机推荐
Move a string of numbers backward in sequence
Select sort and insert sort
Tensorflow2 keras 分类模型
"Redis source code series" learning and thinking about source code reading
Flink-使用流批一体API统计单词数量
Matplotlib剑客行——初相识Matplotlib
1、 QT's core class QObject
Talk about the secret of high performance of message queue -- zero copy technology
gocv图片读取并展示
C# 将网页保存为图片(利用WebBrowser)
Right click menu of QT
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
Linux binary installation Oracle database 19C
WSL安装、美化、网络代理和远程开发
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Qunhui NAS configuring iSCSI storage
WSL installation, beautification, network agent and remote development
Sentinel reports failed to fetch metric connection timeout and connection rejection