我想在 POST 请求中使用相同的正文进行重定向。
来自 GO 来源 (client.go)
func redirectBehavior(reqMethod string, resp *Response, ireq *Request) (redirectMethod string, shouldRedirect, includeBody bool) {
switch resp.StatusCode {
case 301, 302, 303:
redirectMethod = reqMethod
shouldRedirect = true
includeBody = false
// RFC 2616 allowed automatic redirection only with GET and
// HEAD requests. RFC 7231 lifts this restriction, but we still
// restrict other methods to GET to maintain compatibility.
// See Issue 18570.
但有时服务器会为 POST 请求返回 302 Redirect,这意味着我需要将相同的正文发送到另一个位置。
在这种情况下我该怎么办?
func FollowRedirectForPost() {
client := &http.Client{}
req, _ := http.NewRequest(http.MethodPost, "example.com/test", strings.NewReader(url.Values{
"key": {"value"},
"key1":{"value1"},
}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client.Do(req) // If server returns 302 Redirect so it means I need make the same request with the same body to
// a different location. Just imagine i replaced "example.com/test" to "example.com/redirect_url"
}
慕的地6264312
潇潇雨雨