Back to articles
April 4, 2025

Jenkins API: curl with CSRF crumb and token

Create CSRF Crumb and save to Environment Variable $CRUMB

CRUMB=$(wget -q --auth-no-challenge --user JenkinsAdmin --password $PASSWORD --output-document - 'http://34.221.166.10:9090/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
$ echo $CRUMB

Jenkins-Crumb:12345c720376d5e9edc957ddbb0027a88b639eb74888adafc54615bfbf7c8c5

Save JenkinsAdmin token to $TOKEN

TOKEN=12345efe5700dd9a3334f8581885d498a0

Start build on existing job 'pipeline1' $I returns html response

curl -I -X POST http://JenkinsAdmin:$TOKEN@34.221.166.10:9090/job/pipeline1/build -H "$CRUMB"

Copy a job's config.xml to local 'jobConfig.xml' file

curl -X GET http://JenkinsAdmin:$TOKEN@34.221.166.10:9090/job/pipeline1/config.xml -H "$CRUMB" -o ./jobconfig.xml

Create new job from saved 'jobConfig.xml' file

curl -s -X POST http://JenkinsAdmin:$TOKEN@34.221.166.10:9090/createItem?name='job/backend/copyone' --data-binary @jobconfig.xml -H "$CRUMB" -H "Content-Type:text/xml"

Create new job from jobconfig.xml into folder (note added path after servername:port)

curl -s -X POST 'http://JenkinsAdmin:12345efe5700dd9a3334f8581885d498a0@34.221.166.10:9090/job/backend/job/tomcat/createItem?name=copyfour' --data-binary @jobconfig.xml -H "$CRUMB" -H "Content-Type:text/xml"
Loading comments...