当前位置:网站首页>[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 !
边栏推荐
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- 【Go实战基础】gin 如何设置路由
- libusb的使用
- Flink - use the streaming batch API to count the number of words
- Oracle related statistics
- 队列的基本概念介绍以及典型应用示例
- Minecraft module service opening
- Avoid breaking changes caused by modifying constructor input parameters
- Webflux responsive programming
- Minecraft install resource pack
猜你喜欢
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Move a string of numbers backward in sequence
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
Minecraft plug-in service opening
[go practical basis] how can gin get the request parameters of get and post
「Redis源码系列」关于源码阅读的学习与思考
oracle修改数据库字符集
Kubernetes deploys Loki logging system
Troubleshooting and handling of an online problem caused by redis zadd
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
随机推荐
小米电视不能访问电脑共享文件的解决方案
Web技术发展史
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
寻找链表中值域最小的节点并移到链表的最前面
Leetcode sword finger offer brush questions - day 22
C # save web pages as pictures (using WebBrowser)
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
C call system sound beep~
win10使用docker拉取redis镜像报错read-only file system: unknown
《统计学习方法》——第五章、决策树模型与学习(上)
使用IBM MQ远程连接时报错AMQ 4043解决思路
WSL installation, beautification, network agent and remote development
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
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
队列的基本概念介绍以及典型应用示例
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
First week of JS study
Gocv boundary fill
Qunhui NAS configuring iSCSI storage