当前位置:网站首页>[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 !
边栏推荐
- Tensorflow2 keras 分类模型
- WSL installation, beautification, network agent and remote development
- Tensorflow2 keras classification model
- C language - Blue Bridge Cup - 7 segment code
- Multi version concurrency control mvcc of MySQL
- How to realize asynchronous programming in a synchronous way?
- 机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
- Solution of Xiaomi TV's inability to access computer shared files
- oracle修改数据库字符集
- [go practical basis] how to bind and use URL parameters in gin
猜你喜欢

汉诺塔问题的求解与分析

Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain

【Go实战基础】gin 如何验证请求参数

Solution and analysis of Hanoi Tower problem

Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method

Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers

Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?

WSL installation, beautification, network agent and remote development

During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`

Linux安装Oracle Database 19c
随机推荐
[go practical basis] how to bind and use URL parameters in gin
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
Linux二进制安装Oracle Database 19c
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
Troubleshooting and handling of an online problem caused by redis zadd
选择排序和插入排序
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
Leetcode sword finger offer brush questions - day 23
WSL安装、美化、网络代理和远程开发
「Redis源码系列」关于源码阅读的学习与思考
[go practical basis] how to set the route in gin
C#钉钉开发:取得所有员工通讯录和发送工作通知
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
Solution and analysis of Hanoi Tower problem
History of Web Technology
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
聊聊消息队列高性能的秘密——零拷贝技术
Npoi export word font size correspondence
Linux binary installation Oracle database 19C