当前位置:网站首页>[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 !
边栏推荐
- Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
- 机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
- C language implementation of mine sweeping game
- QT -- how to set shadow effect in QWidget
- MYSQL安装出现问题(The service already exists)
- Kubernetes deploys Loki logging system
- Select sort and insert sort
- Linux安装Oracle Database 19c RAC
- I've taken it. MySQL table 500W rows, but someone doesn't partition it?
- Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
猜你喜欢

Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system

Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed

Linux安装Oracle Database 19c

oracle修改数据库字符集

Flink - use the streaming batch API to count the number of words

2022/2/13 summary

Matplotlib剑客行——容纳百川的艺术家教程

Don't spend money, spend an hour to build your own blog website

C#钉钉开发:取得所有员工通讯录和发送工作通知

Talk about the secret of high performance of message queue -- zero copy technology
随机推荐
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
Troubleshooting and handling of an online problem caused by redis zadd
[go practical basis] how can gin get the request parameters of get and post
Solution of Xiaomi TV's inability to access computer shared files
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
commands out of sync. did you run multiple statements at once
oracle修改数据库字符集
Find the node with the smallest value range in the linked list and move it to the front of the linked list
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
Kubernetes deploys Loki logging system
寻找链表中值域最小的节点并移到链表的最前面
Function ‘ngram‘ is not defined
kubernetes部署loki日志系统
Redis zadd导致的一次线上问题排查和处理
WSL installation, beautification, network agent and remote development
Hengyuan cloud_ Can aiphacode replace programmers?
Minecraft plug-in service opening
Move a string of numbers backward in sequence