Skip to content

Instantly share code, notes, and snippets.

@niski84
Created January 6, 2023 08:01
Show Gist options
  • Save niski84/adc7b2af9f14862ac5d5f2bdcb1ed85b to your computer and use it in GitHub Desktop.
Save niski84/adc7b2af9f14862ac5d5f2bdcb1ed85b to your computer and use it in GitHub Desktop.
jenkins print login error
package main
import (
"fmt"
"io/ioutil"
"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()
// Print the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
// 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