imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.25k stars 347 forks source link

对request.RawURL 字段的疑问 #365

Closed N0el4kLs closed 2 months ago

N0el4kLs commented 3 months ago

大佬你好,

Request.RawURL 的值是直接来自于 Send() 方法传入的地址。如果使用 SetQueryString() 等方法设置查询字符,并不会影响到 RawURL 字段。

这里想请教大佬两个问题:

  1. RawURL 这个字段表示的是什么意思?
  2. 如果我想要获取到包括 query 参数在内的完整请求 URL 地址,我应该怎么做?
imroc commented 3 months ago

RawURL 就是发请求是直接传入的url字符串, 比如 Get(url) 中的url。

可以用Client中间件获取完整URL

package main

import (
    "fmt"

    "github.com/imroc/req/v3"
)

func main() {
    c := req.C().
        WrapRoundTripFunc(func(rt req.RoundTripper) req.RoundTripFunc {
            return func(req *req.Request) (resp *req.Response, err error) {
                fmt.Println(req.URL.String())
                return rt.RoundTrip(req)
            }
        })

    c.R().SetQueryParam("test", "1").MustGet("http://httpbin.org/get")
}
N0el4kLs commented 2 months ago

谢谢大佬解答疑惑🙏。ps:主要是遇到的好多RawURL字段都表示完整请求地址,习惯了