I want to follow redirect with the same body for POST request.
From GO sources (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.
But sometimes server returns 302 Redirect for POST request that means I need to send the same body to another location.
What should I do in this situation?
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"
}