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

Is it possible to support cookiejar?

systemview2018 opened this issue · comments

Does the following code meet your needs

package main

import (
	"fmt"
	"github.com/guonaihong/gout"
	"net/http"
)

func main() {

	// === 发送多个cookie ====
	err := gout.
		// :8080/cookie是http://127.0.0.1:8080/cookie的简写
		GET(":8080/cookie").
		//设置debug模式
		Debug(true).
		SetCookies(
			//设置cookie1
			&http.Cookie{
				Name:  "test1",
				Value: "test1",
			},
			//设置cookie2
			&http.Cookie{
				Name:  "test2",
				Value: "test2",
			},
		).
		Do()

	if err != nil {
		fmt.Println(err)
		return
	}

	// === 发送一个cookie ===
	err = gout.
		// :8080/cookie/one是http://127.0.0.1:8080/cookie/one的简写
		GET(":8080/cookie/one").
		//设置debug模式
		Debug(true).
		SetCookies(
			//设置cookie1
			&http.Cookie{
				Name:  "test3",
				Value: "test3",
			},
		).
		Do()
	fmt.Println(err)

}

if Client (*http.Client) is settable, It would be better.

jar, _ := cookiejar.New(nil)
client := &http.Client{
	// example, custom cookie jar implements 
	Jar: jar,
	// example, ignore self-signed certificate 
	Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, 
}
err := gout.GET(":8080").Debug(true).
 setClient(client).
 Do()
fmt.Println(err)

thank you!
Use the New interface to set http.Client

jar, _ := cookiejar.New(nil)
client := &http.Client{
	// example, custom cookie jar implements 
	Jar: jar,
	// example, ignore self-signed certificate 
	Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, 
}
err := gout.New(client).GET(":8080").Debug(true). Do()
fmt.Println(err)

Thanks for your answer.

@guonaihong

这样的话这个(MaxIdleConnsPerHost)得设置

@cute-angelia 是的,因为自定义的http.Client连接池。
这块我再确认下,我记得不设置MaxIdleConnsPerHost。默认值为2。