Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: HTTP PUT Request with separate values in csv

by Veltro (Hermit)
on May 16, 2018 at 16:52 UTC ( #1214670=note: print w/replies, xml ) Need Help??

in reply to HTTP PUT Request with separate values in csv

I just have to say it is very difficult to read your code. Maybe you can indent it properly. People would be much more inclined to try and help you. Also, are we supposed to know what is in 'example.xml'. Posting complete code listings is also not very useful. Try to simplify your question. Reading your code I am absolutely clueless in what you are actually try to do.

Nevertheless to help you on your way a little bit, try to focus on the actual problem. What is the problem? Is the problem in the part that examines the two csv files or is it in the server request. As a matter of fact that is where I would look first.

Create a small sample code for testing and once you have that part working, then start working on the rest of your code.

For example

I suggest you use strict and warnings, it will help you identify problems in your code much sooner.

use strict ; use warnings ;
my $xml = XML::Twig->new()->parsefile('example.xml') ; $xml-> ... ; $xml -> get_xpath('//ipaddress',0) -> set_text("<IP HERE>") ;

Write a test to check if the previous instruction worked. I don't know, are these of HTML::Element XML::Element? If so then print $xml->as_text() print $xml->as_XML could work. Once you know this worked, then move on:

my $uri="https://hostname:9060/ers/config/networkdevice/<ID HERE>"; my $req = HTTP::Request->new( ... $req->content($xml->sprint) ; # Strange, why is there another $xml->sp +rint here? $req->...

Now try to find more ways to introspect $req and see if it contains everything you want. Try to do a request and see if it works:

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

edit: forgot to add the Twig parsefile instruction.

Replies are listed 'Best First'.
Re^2: HTTP PUT Request with separate values in csv
by StayCalm (Novice) on May 17, 2018 at 08:59 UTC

    Sorry,I've changed my posting. And about your question:
    I think my Problem is to separate my hush in right way:

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

    There is no Problem with the server
    I've tested it already with this code:

    open ( my $input_1, '<', 'output1.csv' ) or die $!; while ( <$input_1> ) { chomp; my ($name_1, $ip_1) = 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; (my $id, $first{$name}, $second{$name}) = split /,/; $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");

    It works, but it works wrong!This code updates every device (also identical devices) And I have this message.After Update it tries to do smth(I do not understand what)and I become 400,405 Http Status. I also get warnings:

    UPDATE need be done for 200 OK 400 Bad Request Use of uninitialized value $id in concatenation (.) or string at My_Fi +le line 65, <$_[...]> line 3. 405 Method Not Allowed Use of uninitialized value $id in concatenation (.) or string at My_Fi +le line 65, <$_[...]> line 4. 405 Method Not Allowed 400 Bad Request example ip address

    Line 65: my $uri="$id +";

      OK, but you are still not trying to do what I suggested.

      Minimize your code and create much smaller pieces of test code so you will be able to learn how to debug your own code.

      I'll give you another example what I am talking about. In the next piece of text code I have found multiple problems, so play around with it a little bit and try to fix it yourself:

      use strict; use warnings; use Data::Dumper; 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>; print Dumper( \%first ) ; open $fh, '<', $file_2 or die "Can't open $file_2: $!"; my %second = map { chomp; (split /\s*,\s*/)[1,2] } <$fh>; print Dumper( \%second ) ; foreach my $name (sort keys %first) { if (not exists $second{$name}) { print "Devices should be added: $name\n"; next; } if ($first{$name} eq $second{$name}) { print "Match found $name, $first{$name}\n"; } else { print "UPDATE need be done for $second{$name}\n"; open ( my $input_1, '<', 'output1.csv' ) or die $!; while ( <$input_1> ) { chomp; print " input_1 = $_\n" ; my ($name_1, $ip_1) = split /,/; print " (1) $name, $ip_1\n" ; (my $id, $first{$name}, $second{$name}) = split /,/; print " (2) $id, $first{$name}, $second{$name}\n" ; } } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214670]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-03-01 23:33 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (28 votes). Check out past polls.