如何使用Python请求模块和TeamCity API来触发构建?

2024-10-01 07:43:34 发布

您现在位置:Python中文网/ 问答频道 /正文

TeamCity 9.x DocumentationTriggering a Build部分有一个旋度示例:

curl -v -u user:password http://teamcity.server.url:8111/app/rest/buildQueue --request POST --header "Content-Type:application/xml" --data-binary @build.xml

我想知道如何将它转换成等效的Python脚本(使用来自requests模块的POST请求)?在


顺便说一句,我尝试了下面的Python脚本,但是得到了这样一个响应代码400 (Bad Request)

url = "http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers = {'Content-Type': 'application/json'}
data = json.dumps({'buildTypeId': 'MyTestBuild'})
r = requests.post(url, headers=headers, data=data, auth=("username", "password"), timeout=10)
print "r = ", r

>> r =  <Response [400]>

如果将headers中的Content-Type更改为Accept,则得到另一个响应代码415 (Unsupported Media Type)

^{pr2}$

Tags: 脚本restapphttpurldataapplicationtype
2条回答

FYI json请求适用于TeamCity 10。在

触发构建的文档显示您需要发送XML,而不是JSON:

<build>
    <buildType id="buildConfID"/>
</build>

teamcityrestapi有点复杂;有些方法同时接受XML和JSON,有些只接受XML。这是后一种方法。它们将根据您设置的Accept报头来响应XML或JSON。在

将上面的内容与所需的构建ID一起发送;对于只需使用模板的XML文档:

^{pr2}$

注意,我使用了^{} function来确保build_id的值被正确地引用为XML属性。在

这将生成XML;如果要处理JSON响应,请将'Accept': 'application/json'添加到headers字典中。在

相关问题 更多 >