如何防止”go-echo”中的”+”转义

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

How to prevent "+" from escaping in go-echo

问题

我正在一个项目中使用https://github.com/labstack/echo。我正在使用c.QueryParam来解析查询参数及其值。其中一个值包含一个+符号,它会将其转换为一个空格字符(这是正确的)。然而,我想保留值中的+字符。

例如:http://localhost:8080?param=test+test

fmt.Println(c.QueryParam("param"))

现在它输出test test。然而,我希望输出为test+test。是否可以使用c.QueryParam实现这一点?

英文:

I am using https://github.com/labstack/echo in one of the projects. I am using c.QueryParam to parse the query parameter and its values. One of the values contains a + symbol in it and it converts them to a space character (which is correct). However, I would like to retain the + character in the value.

Ex: http://localhost:8080?param=test+test

fmt.Println(c.QueryParam("param"))

Right now it outputs test test. However, I am expecting the output as test+test. Is it possible to achieve it using c.QueryParam?

答案1

得分: 1

你可以获取原始查询字符串,然后解析参数及其值。

func hello(c echo.Context) error {
    // 获取原始查询字符串
    fmt.Println(c.Request().URL.RawQuery)
    return c.String(http.StatusOK, "Hello, World!")
}

然后你可以使用strings.Split(rawQuery, "=")来获取参数及其值。

英文:

You can get the raw query and then parse the parameter and it's value

func hello(c echo.Context) error {
    //to get the raw query
	fmt.Println(c.Request().URL.RawQuery)
	return c.String(http.StatusOK, "Hello, World!")
}

Then you can use strings.split(rawQuery,"=") to get the parameter and it's value.

答案2

得分: 0

你可以编写一个自定义的辅助函数,如下所示:

func CustomQueryParam(c echo.Context, name string) string {
    qParams := make(map[string]string)
    for _, singleQueryParamStr := range strings.Split(c.QueryString(), "&") {
        val := strings.Split(singleQueryParamStr, "=")
        qParams[val[0]] = val[1]
    }
    return qParams[name]
}

func TestGet(c echo.Context) error {
    param := CustomQueryParam(c, "param")
    fmt.Println(param)

    return c.JSON(http.StatusOK, map[string]interface{}{
        "message": "request is successful",
    })
}

现在,输出结果符合您的期望。它打印出 test+test。但实际上 CustomQueryParam 函数是做什么的呢?

好的,让我们来探索一下。假设 API 调用如下:

http://localhost:8080?param=test1+test2&param2=test3

CustomQueryParam 函数将接受一个 echo.Context 实例和查询参数名称作为函数参数。

然后,在循环中,整个查询字符串,即在我们的例子中为 param=test1+test2&param2=test3,通过 & 进行拆分,并存储在由每个查询参数字符串组成的字符串切片中([]string{"param=test1+test2", "param2=test3"})。

然后,我们遍历每个查询参数字符串,并再次拆分为一个包含两个值的字符串切片,其中第一个值是参数名称,第二个值是参数值。例如,对于第一个查询参数字符串,结果输出如下:

"param=test1+test2" => []string{"param", "test1+test2"}

然后,将第一个值(参数名称)分配为映射键,将第二个值(参数值)分配为映射值。

完成上述过程后,通过查询参数名称(即此函数的参数)获取的映射值将被返回。

这个自定义函数的一个有趣的特点是,如果找不到查询参数,它将返回空字符串。

英文:

You can write a custom helper function like below -

func CustomQueryParam(c echo.Context, name string) string {
	qParams := make(map[string]string)
	for _, singleQueryParamStr := range strings.Split(c.QueryString(), "&") {
		val := strings.Split(singleQueryParamStr, "=")
		qParams[val[0]] = val[1]
	}
	return qParams[name]
}

func TestGet(c echo.Context) error {
	param := CustomQueryParam(c, "param")
	fmt.Println(param)

	return c.JSON(http.StatusOK, map[string]interface{}{
		"message": "request is successful",
	})
}

Now, the output is as your expection. It prints test+test. But what actually CustomQueryParam do?

Okay, let's explore the insight. Suppose, the api call is -

http://localhost:8080?param=test1+test2&param2=test3

The CustomQueryParam function will take an echo.Context instance and the query param name as function parameters.

Then, inside for loop the whole query string i.e. in our case which is param=test1+test2&param2=test3 is split by & and stored into string slice made by each query param string ([]string{"param=test1+test2", "param2=test3"}).

After that, we iterate over each query param string and again split into a string slice of two values having first value as param name and second value as param value. For example, for first query param string, the resultant output is like below -

"param=test1+test2" => []string{"param", "test1+test2"}

Then, the first value (param name) is assigned as map key and second value (param value) as map value.

After finishing above process for every query string, the map value by query param name (which is the parameter of this function) is returned.

One of the interesting fact about this custom function is that it returns empty string if query param is not found.

huangapple
  • 本文由 发表于 2022年12月21日 07:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/74869893.html
匿名

发表评论

匿名网友

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

确定