首页 > 解决方案 > 如何在没有 URL 解码的情况下读取 fasthttp 中的查询参数

问题描述

我正在使用fasthttpGET处理Go 中的请求。

此请求中的查询参数test.%2A%2Ftoday%2F.%2A

我正在使用 POSTMAN 创建请求,生成的 URL 是:

http://localhost:3000/apiname/?test=.%252A%252Ftoday%252F.%252A

ctx.QueryArgs().Peek("test")给了我 .*/today/.*而不是原来的.%2A%2Ftoday%2F.%2A

我知道我无法对请求 URL 进行部分编码/解码。有没有办法按原样获取原始参数?

标签: gofasthttp

解决方案


你确定吗?我刚刚测试了它,我得到了你想要的结果。

这是最小的工作示例:

package main

import (
    "fmt"
    "log"

    "github.com/fasthttp/router"
    "github.com/valyala/fasthttp"
)

func Test(ctx *fasthttp.RequestCtx) {
    ctx.Response.SetBodyString(string(ctx.QueryArgs().Peek("test")))
    fmt.Println(string(ctx.QueryArgs().Peek("test")))
}

func main() {

    r := router.New()
    r.GET("/test", Test)
    log.Fatal(fasthttp.ListenAndServe("127.0.0.1:8080", r.Handler))
}

这是 GET 请求后的命令行输出:

$ go run main.go                                                                                                                
.%2A%2Ftoday%2F.%2A

这是邮递员的回应:

在此处输入图像描述


推荐阅读