guonaihong / gout

gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

怎么获取响应头和Cookie?

ShineJstar opened this issue · comments

我要做个爬虫模拟登陆,要怎么获取响应和Cookie,查了所有函数都不能方便获取,只能通过Bind(req *http.Request, resp *http.Response)函数太复杂了。

解析响应头里面的Set-Cookie字段,是否是你需要的方法?

type RspHeader struct {
	Cookie  string `header:"Set-Cookie"`
}

func main() {
	
	var header RspHeader
	err := gout.GET("localhost:8080").
		BindHeader(&header).
		Do()
        fmt.Println(header)

谢谢,这样也可以,如果有BindCookie能取出[]http.Cookie就更好了,SetCookies也就能直接用了。

这样取出的cookie只有一个,用[]string能取出多个cookie,但SetHeader(core.H{"Cookie": header.Cookie})在请求时候序列化成json数组了,服务端识别不了cookie了,转[]http.cookie来解决又增加麻烦性了

非常棒,现在完美解决我的问题了,感谢 :)

thx :)