Gin 学习之接收参数和读取 reader

简介: Gin 学习之接收参数和读取 reader

01

PostForm


form 传参:

func (*gin.Context).PostForm(key string) string

PostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns an empty string `("")`.


urlencoded form 和 multipart form:


urlencoded form 数据被编码成以 '&' 分隔的键-值对, 同时以 '=' 分隔键和值. 非字母或数字的字符会被 percent-encoding。


multipart form 支持二进制数据。


02

DefaultPostForm


form 传参:

func (*gin.Context).DefaultPostForm(key string, defaultValue string) string

DefaultPostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns the specified defaultValue string.


更多信息,查看 PostForm() and GetPostForm()。


03

Query


URL 传参

func (*gin.Context).Query(key string) string

Query returns the keyed url query value if it exists, otherwise it returns an empty string `("")`.


它是 c.Request.URL.Query().Get(key) 的一种快捷方式。


示例:

GET /path?id=1234&name=Manu&value=
c.Query("id") == "1234"
c.Query("name") == "Manu"
c.Query("value") == ""
c.Query("wtf") == ""


04

DefaultQuery


URL 传参

func (*gin.Context).DefaultQuery(key string, defaultValue string) string

DefaultQuery returns the keyed url query value if it exists, otherwise it returns the specified defaultValue string.


更多信息,查看 Query() and GetQuery()。


示例:

GET /?name=Manu&lastname=
c.DefaultQuery("name", "unknown") == "Manu"
c.DefaultQuery("id", "none") == "none"
c.DefaultQuery("lastname", "none") == ""


05

PostFormMap


form 传参:


func (*gin.Context).PostFormMap(key string) map[string]string

PostFormMap returns a map for a given form key.


06

QueryMap


URL 传参:


func (*gin.Context).QueryMap(key string) map[string]string

QueryMap returns a map for a given query key.


示例代码:

640.png


请求示例:

curl --location --request POST 'http://localhost:8081/user?tel=13800138000&email=lucy@gmail.com&sex=girl&score[a]=66&score[b]=88' \
--form 'name=lucy' \
--form 'age=17' \
--form 'level[chinese]=1' \
--form 'level[english]=2'


07

Param


URI 传参:

func (*gin.Context).Param(key string) string

Param returns the value of the URL param.


它是 c.Params.ByName(key) 的一种快捷方式。


示例:

router.GET("/user/:id", func(c *gin.Context) {
    // a GET request to /user/john
    id := c.Param("id") // id == "john"
})


08

DataFromReader


func (*gin.Context).DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)

DataFromReader writes the specified reader into the body stream and updates the HTTP code.


示例代码:





目录
相关文章
|
语音技术
发送octet-stream格式的请求返回乱码处理
octet-stream格式的请求返回乱码处理
|
11天前
|
消息中间件 Kafka Go
从Go channel中批量读取数据
从Go channel中批量读取数据
|
1月前
|
JavaScript
文本,前后端数据交互,简单请求,如何去给data数据赋值,在mounted()里赋值,利用axios发送的请求,res就是数据集,就是后端的数据,this.users = res.data.data
文本,前后端数据交互,简单请求,如何去给data数据赋值,在mounted()里赋值,利用axios发送的请求,res就是数据集,就是后端的数据,this.users = res.data.data
|
JSON 中间件 Go
golang gin 框架读取无法用 body 传递的表单参数
golang gin 框架读取无法用 body 传递的表单参数
golang gin 框架读取无法用 body 传递的表单参数
|
存储 缓存 安全
PHP的passthru() 函数:执行一个命令,并将输出直接发送到输出缓冲区,缓冲区是干什么的?为什么要发送到缓冲区?
PHP的passthru() 函数:执行一个命令,并将输出直接发送到输出缓冲区,缓冲区是干什么的?为什么要发送到缓冲区?
270 0
|
JSON JavaScript 数据格式
封装读取 data.json 文件的方法|学习笔记
快速学习封装读取 data.json 文件的方法
157 0
|
Go
Go 接收未知大小文件并转为对应大小的byte字节流
Go 接收未知大小文件并转为对应大小的byte字节流
307 0
|
C# 数据库
C#编程-69:DataReader和DataSet读取数据库内容示例_
C#编程-69:DataReader和DataSet读取数据库内容示例_
160 0
Jmeter组件-Random CSV Data Set Config参数化CSV随机读取文件
Jmeter组件-Random CSV Data Set Config参数化CSV随机读取文件
Jmeter组件-Random CSV Data Set Config参数化CSV随机读取文件
Jayway - context.read 读取数据以及类型转换问题
Jayway - context.read 读取数据以及类型转换问题
104 0