gin context和官方context_Gin 学习之接收参数和读取 reader

c3b262d1025ad77c8372c9640b0249a3.png

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. 

示例代码:

981b3961a60892b5b1607986130573af.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.

示例代码:

6eefdd1760a548b8495df269e9fa0f94.png


5afb181b15d76b9fdabf6ac738fae7f5.png

推荐阅读:

Gin 学习之安装和快速启动

Gin 学习之响应处理

Gin 学习之 HTTP 请求方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值