Skip to content

Commit

Permalink
Merge pull request imroc#238 from M-Cosmosss/dev/get-cookie
Browse files Browse the repository at this point in the history
feat: add client func GetCookies
  • Loading branch information
imroc authored May 18, 2023
2 parents d25aa32 + a10478e commit b055831
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
"golang.org/x/net/publicsuffix"
"io"
"net"
"net/http"
Expand All @@ -20,6 +17,11 @@ import (
"reflect"
"strings"
"time"

"golang.org/x/net/publicsuffix"

"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
)

// DefaultClient returns the global default Client.
Expand Down Expand Up @@ -942,6 +944,18 @@ func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
return c
}

// GetCookies get cookies from the underlying `http.Client`'s `CookieJar`.
func (c *Client) GetCookies(url string) ([]*http.Cookie, error) {
if c.httpClient.Jar == nil {
return nil, errors.New("cookie jar is not enabled")
}
u, err := urlpkg.Parse(url)
if err != nil {
return nil, err
}
return c.httpClient.Jar.Cookies(u), nil
}

// ClearCookies clears all cookies if cookie is enabled.
func (c *Client) ClearCookies() *Client {
if c.httpClient.Jar != nil {
Expand Down

0 comments on commit b055831

Please sign in to comment.