Home > Backend Development > Golang > How to Get the Final URL After HTTP Redirection in Go?

How to Get the Final URL After HTTP Redirection in Go?

Linda Hamilton
Release: 2024-11-18 07:45:03
Original
226 people have browsed it

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template