【问题标题】:HTTP PUT Request with separate values in csv [duplicate]在 csv 中具有单独值的 HTTP PUT 请求 [重复]
【发布时间】:2018-07-16 13:54:01
【问题描述】:

我想比较 csv 中的值单独。在此代码示例中,我比较哈希值Compare values in csv files,但对于我的 PUT 和 POST 请求我需要单独的值

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

我的结果应该是:

  • 不使用 Test1(因为它已经在系统 2 中)
  • 更新Test2(因为现在我有不同的ip地址)
  • 添加Test3,因为我在二级系统中没有。

     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";
                  }
         }
     }
    

    }

打印如下:

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

其实我在等:

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

因为要对1235,Test2,10.56.4.10 进行PUT request,所以ipaddress 应该从10.56.4.10 更改为10.56.4.14

但我认为我的问题是,我错误地区分了我的价值观。

【问题讨论】:

    标签: perl csv parsing hash


    【解决方案1】:
    my $id, $first{$name}, $second{$name} = split /,/; 
    

    将生成:

    Useless use of hash element in void context ....
    

    您在运行代码时是否真的阅读过错误消息?

    应该是:

    ( my $id, $first{$name}, $second{$name} ) = split /,/;
    

    【讨论】:

    • 它现在从output2.csv 获取值并进行更新(无更改)。我需要output1.csv 的ipaddress 以Test2 name 更改设备。 output2.csv: 1234,Test1, 10.56.7.13 1235,Test2, 10.56.4.14
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2011-01-28
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多