Jenkins REST API Create job
Clash Royale CLAN TAG#URR8PPP
Jenkins REST API Create job
I'm creating a new job in Jenkins using the REST API. I tried the below curl command lines, but they are throwing an error
curl -i -X POST --user "admin:<API token>" --data-binary "@C:mylocalconfig.xml" -H "Content-Type: text/xml" http://localhost:8080/createItem?name=NewJob
curl -X POST -u <username>:<pass> -H "Content-Type:application/xml" -d "@C:mylocalconfig.xml" "http://localhost:8080/createItem?name=AA_TEST_JOB3"
Error:
HTTP/1.1 403 No valid crumb was included in the request
Date: Fri, 01 Jul 2016 05:25:59 GMT
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 360
Server: Jetty(9.2.z-SNAPSHOT)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /createItem. Reason:
<pre> No valid crumb was included in the request</pre></p><hr><i><small>Power
ed by Jetty://</small></i><hr/>
</body>
</html>
1 Answer
1
Jenkins by default has CSRF Protection enabled which prevents one-click attacks. To invoke the request, you need to obtain the crumb from /crumbIssuer/api/xml
using your credentials and include it into your request.
/crumbIssuer/api/xml
For example:
CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
Then you can create a job (by including the crumb into your header):
curl -X POST -H "$CRUMB" "http://USER:TOKEN@localhost:8080/createItem?name=NewJob"
If the above won't work, check your crumb (echo $CRUMB
) or run curl
with -u USER:TOKEN
.
echo $CRUMB
curl
-u USER:TOKEN
For a more detailed explanation, see: Running jenkins jobs via command line.
When I do this, I get 'Error No Content-Type header set'
– battey
Feb 2 '17 at 23:20
As of 2017, this is the full example: Running jenkins jobs via command line
– BentCoder
Jun 4 '17 at 12:33
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate
– rdupz
Jul 1 '16 at 7:17