我正在使用 REST API 在 Jenkins 中创建一个新作业。我尝试了下面的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 默认有 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 。
关于Jenkins REST API 创建作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137760/