Created
January 6, 2023 07:45
-
-
Save niski84/e35e72ffd7ed1faa0ee1ec75d8599bb2 to your computer and use it in GitHub Desktop.
jenkins login with header set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/http/cookiejar" | |
"net/url" | |
) | |
func main() { | |
// Create an HTTP client with a cookie jar | |
jar, err := cookiejar.New(nil) | |
if err != nil { | |
panic(err) | |
} | |
client := &http.Client{Jar: jar} | |
// Authenticate to Jenkins | |
username := "your-username" | |
password := "your-password" | |
loginURL := "http://jenkins.example.com/j_acegi_security_check" | |
data := url.Values{ | |
"j_username": {username}, | |
"j_password": {password}, | |
} | |
req, err := http.NewRequest("POST", loginURL, strings.NewReader(data.Encode())) | |
if err != nil { | |
panic(err) | |
} | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
// Get the session ID from the cookie | |
for _, cookie := range jar.Cookies(resp.Request.URL) { | |
if cookie.Name == "JSESSIONID" { | |
fmt.Println("Session ID:", cookie.Value) | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment