Skip to content

Commit 7f6cd53

Browse files
committed
Add PrintCookies function to print cookies stored in the HTTP client's cookie jar for a given URL
1 parent ab7e765 commit 7f6cd53

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cookiejar/cookiejar.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"net/http"
1414
"net/http/cookiejar"
15+
"net/url"
1516
"strings"
1617

1718
"github.com/deploymenttheory/go-api-http-client/logger"
@@ -73,3 +74,28 @@ func ParseCookieHeader(header string) *http.Cookie {
7374
}
7475
return nil
7576
}
77+
78+
// PrintCookies prints the cookies stored in the HTTP client's cookie jar for a given URL.
79+
func PrintCookies(client *http.Client, urlString string, log logger.Logger) {
80+
if client.Jar == nil {
81+
log.Error("Cookie jar is not initialized.")
82+
return
83+
}
84+
85+
// Correctly use url.Parse for parsing the URL string
86+
parsedURL, err := url.Parse(urlString)
87+
if err != nil {
88+
log.Error("Failed to parse URL for cookie jar", zap.Error(err))
89+
return
90+
}
91+
92+
cookies := client.Jar.Cookies(parsedURL)
93+
if len(cookies) == 0 {
94+
log.Info("No cookies found for URL", zap.String("url", urlString))
95+
return
96+
}
97+
98+
for _, cookie := range cookies {
99+
log.Info("Cookie", zap.String("Name", cookie.Name), zap.String("Value", cookie.Value))
100+
}
101+
}

0 commit comments

Comments
 (0)