英文:
cannot use baseURL (type *url.URL) as type string in argument to http.Get
问题
我写了一个简单的Web服务器,使用url.ResolveReference将一些相对路径附加到基本URL上。然后,我想使用http.Get()获取结果URL的内容,但问题是http.Get()接受字符串作为参数,而我有一个*url.URL类型的对象。我该如何解决这个问题?
以下是我的代码:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func factHandler(w http.ResponseWriter, r *http.Request) {
str1 := "http://www.meaningfultype.com/"
u1, _ := url.Parse(str1)
str2 := "/clarendon.html"
u2, _ := url.Parse(str2)
u3 := u1.ResolveReference(u2)
fmt.Println("url:", u3)
res, err := http.Get(u3)
if err != nil {
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
contentType := res.Header.Get("Content-Type")
w.Header().Set("Content-Type", contentType)
w.Write(robots)
}
希望能帮到你!
英文:
I wrote a simple web server that appends some relative path to the base URL using url.ResolveReference. And then I want to get the content of resultant URL using http.Get() but the problem is that http.get() take string as the argument and I have an object of type *url.URL.How can I resolve this problem?
My code is given bellow:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func factHandler(w http.ResponseWriter, r *http.Request) {
str1 := "http://www.meaningfultype.com/"
u1, _ := url.Parse(str1)
str2 := "/clarendon.html"
u2, _ := url.Parse(str2)
u3 := u1.ResolveReference(u2)
fmt.Println("url:", u3)
res, err := http.Get(u3)
if err != nil {
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
contentType := res.Header.Get("Content-Type")
w.Header().Set("Content-Type", contentType)
w.Write(robots)
}
答案1
得分: 1
调用u3
的String()
方法,并将返回的字符串传递给http.Get()
:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func factHandler(w http.ResponseWriter, r *http.Request) {
str1 := "http://www.meaningfultype.com/"
u1, _ := url.Parse(str1)
str2 := "/clarendon.html"
u2, _ := url.Parse(str2)
u3 := u1.ResolveReference(u2)
fmt.Println("url:", u3)
res, err := http.Get(u3.String()) // 更改此行
if err != nil {
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
contentType := res.Header.Get("Content-Type")
w.Header().Set("Content-Type", contentType)
w.Write(robots)
}
请注意,这是一段Go语言代码。
英文:
Call the String()
method of u3 and pass the returned string to http.Get()
:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func factHandler(w http.ResponseWriter, r *http.Request) {
str1 := "http://www.meaningfultype.com/"
u1, _ := url.Parse(str1)
str2 := "/clarendon.html"
u2, _ := url.Parse(str2)
u3 := u1.ResolveReference(u2)
fmt.Println("url:", u3)
res, err := http.Get(u3.String()) // Change this line
if err != nil {
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
contentType := res.Header.Get("Content-Type")
w.Header().Set("Content-Type", contentType)
w.Write(robots)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论