【发布时间】:2012-12-14 01:48:12
【问题描述】:
我有一个 perl 脚本通过 REST API 备份我们的 TeamCity 服务器,如下所示:
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw{ POST GET }
# ... code ommitted for brevity ... #
my $url = 'http://teamcity:8080/httpAuth/app/rest/server/backup';
my $req = POST( $url . '?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=' . $filename);
$req->authorization_basic($username, $password);
my $resp = $ua->request($req);
我尝试发布更符合 HTTP:Request 文档的内容,但由于某种原因它失败了,抱怨我没有指定文件名:
# This fails
my $req= POST( $url, [ 'includeConfigs' => 'true',
'includeDatabase' => 'true',
'includeBuildLogs' => 'true',
'fileName' => $filename,
] );
然而,当我查看 TeamCity 的后端 REST 日志时,完整的请求似乎完好无损,并且与上面传递的请求相同。
命令成功日志:
[2012-12-13 15:02:38,574] DEBUG [www-perl/5.805 ] - rver.server.rest.APIController - REST API request received: POST '/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=foo', from client 10.126.31.219, authenticated as jsmith
失败命令日志:
[2012-12-13 14:57:00,649] DEBUG [www-perl/5.805 ] - rver.server.rest.APIController - REST API request received: POST '/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=foo', from client 10.126.31.219, authenticated as jsmith
这两种发出 POST 请求的方法之间是否还有其他隐藏的差异可能导致失败?
更新:这是通过 Data::Dumper 打印时每个请求的结果
成功发布:
$VAR1 = bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'http://teamcity:8080/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=foo')}, 'URI::http' ),
'_headers' => bless( {
'content-type' => 'application/x-www-form-urlencoded',
'content-length' => 0,
'authorization' => 'Basic c3lzQnVpbGRTeXN0ZW1JOnBhaWQuZmFpdGg='
}, 'HTTP::Headers' ),
'_method' => 'POST'
}, 'HTTP::Request' );
POST 失败:
$VAR1 = bless( {
'_content' => 'includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=foo',
'_uri' => bless( do{\(my $o = 'http://teamcity:8080/httpAuth/app/rest/server/backup')}, 'URI::http' ),
'_headers' => bless( {
'content-type' => 'application/x-www-form-urlencoded',
'content-length' => 75,
'authorization' => 'Basic c3lzQnVpbGRTeXN0ZW1JOnBhaWQuZmFpdGg='
}, 'HTTP::Headers' ),
'_method' => 'POST'
}, 'HTTP::Request' );
【问题讨论】:
-
这里没有加起来。您确定第一个日志条目确实对应于失败的命令吗?尝试在失败的命令中使用可识别的内容,例如不同的文件名,并查看它是否会产生您期望的日志条目。
-
如果您在检查和仔细检查相同的内容时只能看到我从头上拔了多少头发... ;-) 日志输出仅显示请求 URI TeamCity 声称已收到...所以肯定有一些更细微的区别。
-
澄清一下:您使用的是
HTTP::Request::Common? -
是 - 使用 HTTP::Request::Common qw{ POST GET };
标签: perl http rest post teamcity