http.NewRequest只允许一个重定向。

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

http.NewRequest allow only one redirect

问题

我来自这个问题,答案适用于我不想要任何重定向的情况:

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

但是在这里,如何允许仅一次重定向呢?

英文:

I came from this question and the answer works if I want no redirect at all:

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

But how can I allow exactly one redirect here?

答案1

得分: 2

根据文档的说明:

参数 req 和 via 分别是即将进行的请求和已经发出的请求,最早的请求排在最前面。

因此,在第一次重定向时,len(via) 的值将为 1。如果在 len(via)>1 时返回错误,那么对于额外的请求,它应该会失败。

英文:

As the documentation states:

> The arguments req and via are the upcoming request and the requests made already, oldest first.

So at the first redirect, len(via) will be 1. If you return error if len(via)>1, it should fail for additional requests.

huangapple
  • 本文由 发表于 2021年10月6日 22:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69467758.html
匿名

发表评论

匿名网友

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

确定