当前位置:网站首页>[go practical basis] how can gin get the request parameters of get and post
[go practical basis] how can gin get the request parameters of get and post
2022-07-02 09:08:00 【Novice practice】
Catalog
3、 ... and 、 Rookie actual combat
GET Method , Add routing parameters and callbacks
POST Method , Add routing parameters and callbacks , Filling parameters
One 、 brief introduction
Actual combat scene : How to use gin obtain GET and POST Request parameters for
Two 、 Knowledge point
- gin route
- http Status code
- obtain GET Request parameters
- obtain POST Request parameters
- obtain POST Form Request parameters
3、 ... and 、 Rookie actual combat
Make arrangements now !
1、 establish go file
package main
// Import package
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"runtime"
)
// The main function
func main() {
// Print using built-in functions
println("Hello", " Rookie actual combat ")
println(" Actual combat scene : ", "gin How to get GET Request parameters ")
// initialization
r := gin.Default()
// GET Method , Add routing parameters and callbacks
r.GET("/g", func(c *gin.Context) {
name := c.Query("name")
// Default parameters
age := c.DefaultQuery("age", "18")
// Back to code and Corresponding parameter Galaxy
c.String(http.StatusOK, "%s - %s \n", name, age)
})
// POST Method , Add routing parameters and callbacks
r.POST("/p", func(c *gin.Context) {
bodyByts, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// Return error message
c.String(http.StatusBadRequest, err.Error())
// Execute exit
c.Abort()
}
// Back to code and Corresponding parameter Galaxy
c.String(http.StatusOK, "%s \n", string(bodyByts))
})
// POST Method , Add routing parameters and callbacks , Filling parameters
r.POST("/p1", func(c *gin.Context) {
bodyByts, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// Return error message
c.String(http.StatusBadRequest, err.Error())
// Execute exit
c.Abort()
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyByts))
name := c.PostForm("name")
age := c.DefaultPostForm("age", "18")
// Back to code and Corresponding parameter Galaxy
c.String(http.StatusOK, "%s,%s,%s\n", name, age, string(bodyByts))
})
// Use package functions to print
fmt.Printf(" edition : %s \n", runtime.Version())
// Start the framework program , Default 8080 port
r.Run()
}
// curl Verification mode
// g
// curl -X GET "http://127.0.0.1:8080/g?name=kitty"
// curl -X GET "http://127.0.0.1:8080/g?name=kitty&age=21"
// p
// curl -X POST "http://127.0.0.1:8080/p" -d '{"name":" Hello , Rookie actual combat "}'
// p1
// curl -X POST "http://127.0.0.1:8080/p1" -d 'name=bob'
// curl -X POST "http://127.0.0.1:8080/p1" -d 'name=bob&age=17'2、 Running results
Hello Rookie actual combat
Actual combat scene : gin How to get GET and POST Request parameters for
[GIN-debug] Listening and serving HTTP on :8080
GET Method , Add routing parameters and callbacks
POST Method , json Format
POST Method , Add routing parameters and callbacks , Filling parameters
Rookie actual combat , Continuous learning !
边栏推荐
- 【Go实战基础】gin 如何设置路由
- Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
- 小米电视不能访问电脑共享文件的解决方案
- 2022/2/14 summary
- 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
- Count the number of various characters in the string
- Minecraft install resource pack
- Kubernetes deploys Loki logging system
- Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
- Nacos download, start and configure MySQL database
猜你喜欢
随机推荐
History of Web Technology
kubernetes部署loki日志系统
Move a string of numbers backward in sequence
Minecraft群組服開服
Qt的右键菜单
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
将一串数字顺序后移
一个经典约瑟夫问题的分析与解答
Oracle 相关统计
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
统计字符串中各类字符的个数
Leetcode sword finger offer brush questions - day 22
Minecraft plug-in service opening
Web技术发展史
使用IBM MQ远程连接时报错AMQ 4043解决思路
【Go实战基础】gin 如何验证请求参数
Openshift deployment application
Openshift container platform community okd 4.10.0 deployment
QT qtimer class
Image transformation, transpose














