如何使用REST + cURL更新TeamCity构build参数

我在TeamCity的一个构buildconfiguration中有一个名为“testing”的configuration参数。 在看了TeamCity REST API文档之后,我可以在Windows上使用以下cURL命令行命令获取有关此参数的信息:

(1) curl -X GET -H "Authorization: Basic (...)" http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/parameters (2) curl -X GET -H "Authorization: Basic (...)" http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/parameters/testing 

响应:

 (1) <?xml version="1.0" encoding="UTF-8" standalone="yes"?><property name="testing" value="11"/></properties> (2) 11 

但是,当我尝试使用以下命令更新此“testing”生成参数时,出现错误消息:

 curl -X PUT -d "1" -H "Authorization: Basic (...)" http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/parameters/testing 

响应:

 Error has occurred during request processing (Unsupported Media Type). Error: javax.ws.rs.WebApplicationException Not supported request. Please check URL, HTTP method and transfered data are correct. 

我已经成功地使用类似的命令来更新相同构buildconfiguration的buildNumberCounter设置:

 curl -X PUT -d "1" -H "Authorization: Basic (...)" http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/settings/buildNumberCounter 

这就是为什么我认为我可以用类似的方式做一个构build参数。 我在这里错过了什么?

更新:

我设法更新“testing”生成参数值为“1”使用提琴手。 我撰写的请求有以下内容:

  • 要求: PUT
  • URL: http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/parameters/testing
  • 请求标头: Authorization: Basic (...)
  • 请求正文: 1

所以上面我的cURL命令的问题可能在-d“1”选项周围。 但是哪里?

更新2:

我不确定这是否有所作为,但是我在Windows 7上使用这个 cURL构build。

作为一种解决方法,我们现在不使用修复失败的cURL命令,而是使用Node.js撰写并向TeamCity发送REST请求。

需要提供给node.exe的脚本如下所示:

 // Equivalent cURL command: // curl -X PUT -d "1" -H "Authorization: Basic (...)" http://teamcity:8080/httpAuth/app/rest/buildTypes/id:bt7/parameters/testing var http = require('http'), options = { hostname: 'teamcity', port: 8080, path: '/httpAuth/app/rest/buildTypes/id:bt7/parameters/testing', method: 'PUT', headers: { 'Authorization': 'Basic (...)' } }, req; req = http.request(options, function(res) { }); // write data to request body req.write('1'); req.end(); 

尽管解决方法很好,但我仍然想知道上面的cURL命令有什么问题?

对于非XML类型的参数,您只需添加:–Header“Content-Type:text / plain”

对于XML参数,您需要将其切换为:–Header“Content-Type:application / xml”

我也很难弄清楚这一点,但我找到了答案。 而不是在前面使用-d和-H。 最后使用–data和–header,如下所示。 我在TeamCity文档中发现了这个文档,这些文档被埋在了“点击展开”的例子中 。

设置内部编号计数器:

 curl -v --basic --user <username>:<password> --request PUT http://<teamcity.url>/app/rest/buildTypes/<buildTypeLocator>/settings/buildNumberCounter --data <new number> --header "Content-Type: text/plain" 

设置编号格式:

 curl -v --basic --user <username>:<password> --request PUT http://<teamcity.url>/app/rest/buildTypes/<buildTypeLocator>/settings/buildNumberPattern --data <new format> --header "Content-Type: text/plain" 

我猜REST API希望XML作为输入,添加

 -H "Content-type:text/xml" 

并把XML作为输入。 如果你有一个XML文件file.xml

 curl -d "@/path/to/file.xml" -H "Content-type:text/xml" (...)