HTTP PUT请求在csv中具有单独的值 [英] HTTP PUT Request with separate values in csv
问题描述
我想在csv中比较单独的值。在此代码示例中,比较散列,比较csv文件中的值 a>但是对于我的PUT和POST请求我需要单独的值。
$ b output1.csv (name,ip) - 主系统
Test1,10.56.7.13
Test2,10.56.4.14
Test3,10.56.5.15
output2.csv (id,name,ip) - 辅助系统
1234,Test1,10.56.7.13
1235,Test2,10.56.4.10
我的结果应该是:
$ b
- 对Test1什么都不做(因为它已经在System 2中)
- 更新Test2(因为现在我有不同的IP地址)
-
添加Test3,因为我没有在辅助系统中使用它。
说需要更新$ second {$ name};
#现在我想在第二个系统中为这些设备发出PUT(更新)请求。
open(my $ input_1,'<','output1.csv')或die $!
while(< $ input_1>){
chomp;
my($ name_1,$ ip_1)= split /,/;
my $ xml = XML :: Twig - >新 - > parsefile('template.xml');
$ xml - > set_pretty_print('indented_a');
open(my $ input_2,'<','output2.csv')或die $!;
while(< $ input_2>){
chomp;
$ b $ my($ id,$ name,$ ip)= split /,/;
$ xml - >根 - > set_att('name',$ name);
$ xml - > get_xpath('// ipaddress',0) - > set_text($ IP_1);
my $ uri =https://主机名:9060 / ers / config / networkdevice / $ id;
my $ req = HTTP :: Request-> new('PUT',$ uri,[Accept =>'application / vnd.com.cisco.ise.network.networkdevice.1.1 + xml',
Content_Type =>'application / vnd.com.cisco.ise.network.networkdevice.1.1 + xml; charset = utf-8'],$ xml-> sprint);
$ req->内容($ xml-> sprint);
$ req-> authorization_basic(user,user);
#向用户代理请求并获得回应
my $ res = $ ua-> request($ req);
#如果($ res-> is_success){
print $ res-> status_line,\\\
;则检查响应结果
;
} else {
print $ res-> status_line,\\\
;
}
}
}
}
这将显示: $ b
找到匹配TEST_1,10.66.12.5
更新需要完成10.66.10.4
200 OK
400错误请求
400错误请求
200 OK
200 OK
400错误请求
应添加设备:TEST_3
其实我在等待:
找到匹配TEST_1,10.66.12.5
UPDATE需要完成10.66 .10.4
$ OK
应添加设备:TEST_3
因为要做 PUT请求
,其中 1235,Test2,10.56.4.10
, ipaddress
应该从 10.56.4.10
更改为 10.56.4.14
。
但我认为我的问题是,我将我的价值观分开了。
my $ id,$ first {$ name},$ second {$ name} = split /,/;
会生成:
无效使用散列元素在void context中....
您是否真的在运行代码时阅读错误消息?
应该是:
(my $ id,$ first { $ name},$ second {$ name})= split /,/;
I want to compare values separate in csv. In this code example I compare hashes, Compare values in csv files but for my PUT and POST request I need separate values.
output1.csv (name, ip) - Primary system
Test1, 10.56.7.13
Test2, 10.56.4.14
Test3, 10.56.5.15
output2.csv (id,name,ip) - Secondary system
1234,Test1, 10.56.7.13
1235,Test2, 10.56.4.10
My result should be:
- do nothing with Test1 (because it is already in System 2)
- update Test2 (because now I have a different ip address)
add Test3, because I do not have it in the secondary System.
say "UPDATE need be done for $second{$name}"; #Now I want to make PUT (Update) request for these devices in secondary systen. open ( my $input_1, '<', 'output1.csv' ) or die $!; while ( <$input_1> ) { chomp; my ($name_1, $ip_1) = split /,/; my $xml = XML::Twig -> new -> parsefile ( 'template.xml' ); $xml ->set_pretty_print('indented_a'); open ( my $input_2, '<', 'output2.csv' ) or die $!; while ( <$input_2> ) { chomp; my ($id, $name, $ip) = split /,/; $xml -> root -> set_att('name', $name); $xml -> get_xpath('//ipaddress',0) -> set_text($ip_1); my $uri="https://hostname:9060/ers/config/networkdevice/$id"; my $req = HTTP::Request->new('PUT', $uri,[Accept=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml', Content_Type=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml; charset=utf-8'], $xml->sprint); $req->content($xml->sprint); $req->authorization_basic("user", "user"); #Pass request to the user agent and get a response back my $res = $ua->request($req); #Check the outcome of the response if ($res->is_success) { print $res->status_line, "\n"; } else { print $res->status_line, "\n"; } } }
}
This prints:
Match found TEST_1, 10.66.12.5
UPDATE need be done for 10.66.10.4
200 OK
400 Bad Request
400 Bad Request
200 OK
200 OK
400 Bad Request
Devices should be added: TEST_3
Actually I was waiting for:
Match found TEST_1, 10.66.12.5
UPDATE need be done for 10.66.10.4
200 OK
Devices should be added: TEST_3
Because to do a PUT request
with 1235,Test2,10.56.4.10
, ipaddress
should be changed from 10.56.4.10
to 10.56.4.14
.
But I think my problem is, that I separate my values wrong.
my $id, $first{$name}, $second{$name} = split /,/;
Will generate:
Useless use of hash element in void context ....
Did you actually read your error messages when running your code?
Should be:
( my $id, $first{$name}, $second{$name} ) = split /,/;
这篇关于HTTP PUT请求在csv中具有单独的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!