【问题标题】:How to POST with Curl to configSubmit Jenkins page如何使用 Curl 发布到配置提交 Jenkins 页面
【发布时间】:2017-03-08 11:32:16
【问题描述】:

我可以用 Python 做到这一点,但不能用 Curl...

$ curl -H "Content-Type: application/json; charset=UTF-8" --data-urlencode   \
   '{ "description": "This is a test job.", "displayName": "#30: Success" }' \
   -n http://localhost/job/playground/30/configSubmit


<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /job/playground/30/configSubmit. Reason:
<pre>    Nothing is submitted</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

【问题讨论】:

  • 我在curl -u &lt;username&gt;:&lt;token&gt; -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data-urlencode "description=hello world" https://jenkins.local/job/&lt;job_name&gt;/&lt;build_id&gt;/configSubmitcurl -u &lt;username&gt;:&lt;token&gt; -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data'{"description": "hello world"}' https://jenkins.local/job/&lt;job_name&gt;/&lt;build_id&gt;/configSubmit 以及命令上的一大堆其他版本中遇到了同样的问题。

标签: curl jenkins jetty http-post jenkins-api


【解决方案1】:

我为此苦苦挣扎了几个小时。最终,this answer to a similar question 让我摆脱了困境。

似乎 Jenkins 期待具有名为 json 的字段的表单数据。最终对我有用的 curl 命令是

curl -X POST -F 'json={"displayName":"name","description":"a short description"}' \
  http://localhost/job/playground/30/configSubmit

【讨论】:

    【解决方案2】:

    @Pete 的答案是正确的,但它足以运行:

    curl -F 'json={"displayName":"name","description":"a short description"}' \ http://localhost/job/playground/30/configSubmit

    再解释一下:

    • curl 中的-F 选项用于发布表单,因此-X POST 不是必需的。
    • 需要json=,相当于设置Content-Type: application/json
    • --data-urlencode(如问题中所用)在使用时会覆盖 -F - 因此不能在此处使用。

    @ebeezer 问题来自于尝试仅设置“description”,而需要同时设置“description”和“displayName”才能使请求成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2019-06-08
      相关资源
      最近更新 更多