Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hallo People. I have one question about HTTP PUT Request with separate values in csv. In this code example I compare hashes, but for my PUT and POST request I need separate values.
For example, I have two output files from different Systems:

output1.csv (name, ip) - Primary system


output2.csv (id,name,ip) - Secondary System
01,TEST_1, 02,TEST_3, 03,TEST_5,

My result should be:
  • I do nothing with Test1 (because it is already in System 2)
  • I should update Test3 (because now I have a different ip address)
  • I should update Test5-->Test4 (because now I have a different names with the same ip adress)
  • I should add Test2, because I do not have it in the secondary System
  • use strict; use warnings; use feature qw(say); use autodie; use XML::Twig; use LWP::UserAgent; use HTTP::Headers; use HTTP::Request; use File::Slurp; use JSON -support_by_pp; use LWP 5.64; use MIME::Base64; use IO::Socket::SSL; use File::Slurp; my ($file_1, $file_2) = ('output1.csv', 'output2.csv'); open my $fh, '<', $file_1 or die "Can't open $file_1: $!"; my %first = map { chomp; split /\s*,\s*/ } <$fh>; open $fh, '<', $file_2 or die "Can't open $file_2: $!"; my %second = map { chomp; (split /\s*,\s*/)[1,2] } <$fh>; #Create a user agent object my $ua = LWP::UserAgent->new(ssl_opts=> { SSL_verify_mode => SSL_VERIFY_NONE(), verify_hostname => 0, }); foreach my $name (sort keys %first) { if (not exists $second{$name}) { say "Devices should be added: $name"; next; } if ($first{$name} eq $second{$name}) { say "Match found $name, $first{$name}"; } else { say "UPDATE need be done for $second{$name}";

    Till this place my code works. I compare my hushes and I see messenges in shell.
    Now I want to make PUT (Update) request for these devices, those are nor identic

    Idee ist to open separate both of files and to add to Secondary the ipaddress or names from Primary System

    To make it this way I Need to take ip(or name) from the device from output1.csv and to make PUT request to secondary System (Update)

    open ( my $input_1, '<', 'output1.csv' ) or die $!; while ( <$input_1> ) { chomp; my ($name_1, $ip_1) = split /,/; (my $id, $first{$name}, $second{$name}) = split /,/; my $xml = XML::Twig -> new -> parsefile ( 'example.xml' ); $xml ->set_pretty_print('indented_a'); open ( my $input_2, '<', 'output2.csv' ) or die $!; while ( <$input_2> ) { chomp; $xml -> root -> set_att('name', $name_1); $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/ ++xml', Content_Type=>'application/ +e.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 #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"; } } } } }

    example.xml - it ist the structure of XML data for update(PUT Request)

    If I want to change Attribute, I add it in this file and make HTTP Request(POST, PUT) Wenn I start my code, I get this message:

    UPDATE need be done for 404 Not Found 404 Not Found 404 Not Found 404 Not Found 404 Not Found 404 Not Found Match found TEST_1,

    But actually I Need this one :

    UPDATE need be done for UPDATE need be done for 200 ok 200 ok Match found TEST_1, Devices should be added: Test2

    In reply to HTTP PUT Request with separate values in csv by StayCalm

    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2021-02-25 02:31 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found