高浪:从 HTTP 响应中获取响应重定向网址

我正在尝试使用 HTTP 发出请求。Go 中的 Get(url),我想在浏览器中打开响应。我正在使用浏览器。打开URL()来启动系统浏览器,但我不知道如何获取响应网址。


在Python中,使用请求库,它是响应对象的一个属性。我可以在浏览器(使用浏览器库)中获取并打开它,如下所示:


response = requests.get(endpoint)

browser.open(response.url)

如何在 Go 中使用 http/net 库来实现此目的?响应对象是不包含该属性的结构。


我正在尝试调用Spotify API来验证应用程序,这需要打开浏览器窗口供用户输入。到目前为止,我已经得到了这个:


func getAuth(endpoint *url.Url) {

    request, _ := http.NewRequest("GET", endpoint.string(), nil)

    client := &http.Client{}

    resp, err := client.Do(request)

    if err != nil {

        panic(err)

    }

    headers := resp.Header

    page, _ := ioutil.ReadAll(resp.Body)

在哪里可以获得响应 URL,或者如何处理响应,以便在浏览器中打开它?


皈依舞
浏览 171回答 2
2回答

POPMUISE

如果有重定向,Go 将更新响应上的结构。Requestresp.Request.URL是你要找的。// Request is the request that was sent to obtain this Response.// Request's Body is nil (having already been consumed).// This is only populated for Client requests.Request *Request

哈士奇WWW

只需从响应标头中获取重定向 URL。redirectURL := resp.Header.Get("Location")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go