Home > Article > Backend Development > How to Get the Final URL After HTTP Redirection in Go?
Retrieving the Final URL after Redirection in HTTP Requests
When utilizing http.NewRequest to initiate HTTP requests, you may encounter the need to extract query strings from the final URL, even when the client encounters a redirect. However, you may not find this information readily available in the Response object.
Solution:
One approach to obtaining the final URL involves employing an anonymous function within the CheckRedirect field of the http.Client struct. This anonymous function serves as a callback executed before each redirect to capture the URL of the request.
Here's an example:
In this script, an anonymous function is assigned to the CheckRedirect field of http.Client. This anonymous function sets the lastUrlQuery variable to the request's URL before each redirect occurs. As a result, you can retrieve the final URL of the request after any redirects have taken place.
The above is the detailed content of How to Get the Final URL After HTTP Redirection in Go?. For more information, please follow other related articles on the PHP Chinese website!