记一次Gin框架使用url参数值获取+号出现空格的排查

记一次Gin框架使用url参数值获取+号出现空格的排查

问题描述:

前几天使用gin框架遇到一个问题,接口请求之后,后台拿到的数据中出现取url参数,url参数值如果有携带+,后台拿到的是一个空格。

经排查客户端请求时如果对url参数没有进行编码直接传递+号过来,go语言的net包会将其替换成空格。+号只有转换成url编码, %2b时,gin框架才能正确的解析出+号。

源码部分

可以查看net/url/url.go这个文件,url.go文件处理参数认为传递过来的是进行url编码之后的参数。
在这个函数QueryUnescape下面有一个PathUnsecape。给出的解释是:PathUnescape与QueryUnescape完全相同,只是它不是 unescape ‘+’ to ’ '(空格)。

// QueryUnescape does the inverse transformation of QueryEscape,
// converting each 3-byte encoded substring of the form "%AB" into the
// hex-decoded byte 0xAB.
// It returns an error if any % is not followed by two hexadecimal
// digits.
func QueryUnescape(s string) (string, error) {
   
	return unescape(s, encodeQueryComponent)
}

// PathUnescape does the inverse transformation of PathEscape,
// converting each 3-byte encoded substring of the form "%AB" into the
// hex-decoded byte 0xAB. It returns an error if any % is not followed
// by two hexadecimal digits.
//
// PathUnescape is identical to QueryUnescape except that it does not
// unescape '+' to ' ' (space).
func PathUnescape(s string) (string, error) {
   
	return unescape(s, encodePathSegment)
}

重点看一下unsecape函数



// unescape unescapes a string; the mode specifies
// which section of the URL string is being unescaped.
func unescape(s string, mode encoding) (string, error) {
   
	// Count %, check that they're well-formed.
	n := 0
	hasPlus := false
	for i := 0; i < len(s); {
   
		switch s[i] {
   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值