查询参数 – 在golang中用空格替换+号

huangapple go评论79阅读模式
英文:

Query Param - Replacing + by blank in golang

问题

我正在使用golang

func (ph *testHandler) GetData() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		params := ctx.Request.URL.Query()
        search = strings.TrimSpace(params.Get("data"))
   } 
}

在这里,我在URL中传递了'test+test',但我得到了'test test'

如何在golang中进行查询解析?

英文:

I am using golang

func (ph *testHandler) GetData() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		params := ctx.Request.URL.Query()
        search = strings.TrimSpace(params.Get("data"))
   } 
}

here I am passing 'test+test' in url but I am getting 'test test'

How can I do query parsing in golang

答案1

得分: 3

如果你查看源代码:

func (u *URL) Query() Values {
        v, _ := ParseQuery(u.RawQuery)
        return v
}

调用了ParseQuery函数,该函数在执行其它操作时以"encodeQueryComponent"模式调用了unescape函数。

这段代码片段是相关的:

            case '+':
                    if mode == encodeQueryComponent {
                            t.WriteByte(' ')
                    } else {
                            t.WriteByte('+')
                    }

这只是RFC兼容的解析方式,这是正常的,你所看到的行为是预期的。

参考链接:https://cs.opensource.google/go/go/+/refs/tags/go1.16.6:src/net/url/url.go;drc=refs%2Ftags%2Fgo1.16.6;l=182

英文:

If you look at the source

func (u *URL) Query() Values {
        v, _ := ParseQuery(u.RawQuery)
        return v
}

Calls ParseQuery which calls unescape in the "encodeQueryComponent" mode
as part of the things it does.

this code fragment is relevant

            case '+':
                    if mode == encodeQueryComponent {
                            t.WriteByte(' ')
                    } else {
                            t.WriteByte('+')
                    }

this is simply RFC compliant parsing, it's normal, the behaviour
you are seeing is expected

see https://cs.opensource.google/go/go/+/refs/tags/go1.16.6:src/net/url/url.go;drc=refs%2Ftags%2Fgo1.16.6;l=182

答案2

得分: 1

浏览器将URL中的+%20符号视为空格字符,因此查询参数中的test+test将具有值test test
为了防止这种情况,您需要在URL中手动添加+字符的URL编码值 - https://example.com?data=test%2Btest

有关其他URL编码值,请参见w3schools

英文:

The browsers treats + and %20 signs in URL as space character so test+test in query parameter will have value test test.
To prevent this you have to manually add URL encoded value of + character literal in the URL - https://example.com?data=test%2Btest

For other URL encoded values see w3schools.

答案3

得分: 1

你的查询参数值中的加号实际上是URL编码的ASCII空格。如果你想在后端获取"test+test",你应该对URL的查询参数值中的加号进行百分号编码,即test%2Btest

以下是一些修复问题的最小服务器代码:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", handle)
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}

func handle(w http.ResponseWriter, r *http.Request) {
	data := r.URL.Query().Get("data")
	w.Header().Set("Content-type", "text/plain")
	fmt.Fprint(w, data)
}

结果:

  • http://localhost:8080/?data=test+test 输出 test test
  • http://localhost:8080/?data=test%2Btest 输出 test+test
英文:

The plus sign in the value of your query parameter is actually an URL-encoded ASCII space. If you want to obtain "test+test" in the backend, you should percent-encode the plus sign in the value of the URL's query param: test%2Btest.

Some minimal server code to fix ideas:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", handle)
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}

func handle(w http.ResponseWriter, r *http.Request) {
	data := r.URL.Query().Get("data")
	w.Header().Set("Content-type", "text/plain")
	fmt.Fprint(w, data)
}

Results:

  • http://localhost:8080/?data=test+test prints test test, whereas
  • http://localhost:8080/?data=test%2Btest prints test+test.

huangapple
  • 本文由 发表于 2021年7月22日 14:58:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/68480455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定