-
I'm trying to get the response headers from a 302 response and so far I'm unable to. It seems that due to the http client returning the redirect error the validators aren't applied, so the headers are not copied. Is there any other way to to this? This is the code I used for testing: package main
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/carlmjohnson/requests"
)
var (
errRedirect = errors.New("redirect intercepted")
)
func main() {
myClient := *http.DefaultClient
myClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errRedirect
}
statusCodes := []int{200, 302}
for _, statusCode := range statusCodes {
headers := map[string][]string{}
err := requests.URL(fmt.Sprintf("http://httpbin.org/status/%d", statusCode)).
Client(&myClient).
CopyHeaders(headers).
CheckStatus(statusCode).
Fetch(context.Background())
if err != nil && !errors.Is(err, errRedirect) {
panic(err)
}
fmt.Printf("Headers with %d: %#v\n", statusCode, headers)
}
} Output:
Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
sralloza
Apr 10, 2024
Replies: 1 comment 2 replies
-
And once again, a little bit of extra investigation time resolved the issue. The solution is to return myClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sralloza
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And once again, a little bit of extra investigation time resolved the issue. The solution is to return
http.ErrUseLastResponse
in CheckRedirect instead of a custom error.