当前位置:网站首页>[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 !
边栏推荐
猜你喜欢
Minecraft插件服开服
Move a string of numbers backward in sequence
汉诺塔问题的求解与分析
Minecraft install resource pack
C nail development: obtain all employee address books and send work notices
Openshift container platform community okd 4.10.0 deployment
History of Web Technology
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
Linux二进制安装Oracle Database 19c
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
Essay: RGB image color separation (with code)
Finishing the interview essentials of secsha system!!!
Oracle修改表空间名称以及数据文件
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
Minecraft install resource pack
kubernetes部署loki日志系统
QT -- how to set shadow effect in QWidget
一个经典约瑟夫问题的分析与解答
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
How to realize asynchronous programming in a synchronous way?
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Minecraft安装资源包
commands out of sync. did you run multiple statements at once
C language implementation of mine sweeping game
Linux安装Oracle Database 19c RAC
我服了,MySQL表500W行,居然有人不做分区?
《统计学习方法》——第五章、决策树模型与学习(上)
Npoi export word font size correspondence