詹金斯REST API创建工作

问题描述 投票:41回答:2

我使用REST API在詹金斯创建新任务。我想下面的curl命令行,但他们抛出一个错误

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"

错误:


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>

jenkins
2个回答
78
投票

詹金斯在默认情况下具有CSRF Protection启用防止one-click attacks。要调用请求,您需要获得使用您的凭据/crumbIssuer/api/xml碎屑并将其包含到您的要求。

例如:

CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

然后,你可以创建一个作业(由包括碎屑进入你的头):

curl -X POST -H "$CRUMB" "http://USER:TOKEN@localhost:8080/createItem?name=NewJob"

如果以上都不行,请检查您的面包屑(echo $CRUMB)或curl运行-u USER:TOKEN

如需更详细的说明,请参见:Running jenkins jobs via command line


-1
投票

如果您使用的邮差消防的要求,使用@kenorb上面的例子,让面包屑enter image description here

创建使用createItem端点enter image description here在根级别的文件夹

要创建与上述使用createItem端点enter image description here创建的文件夹内的子文件夹

© www.soinside.com 2019 - 2024. All rights reserved.