Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how to post an xml using REST::Client

by KSHYAMKUMAR (Acolyte)
on Apr 07, 2016 at 21:19 UTC ( [id://1159843]=perlquestion: print w/replies, xml ) Need Help??

KSHYAMKUMAR has asked for the wisdom of the Perl Monks concerning the following question:

I have a requirement to send an xml using REST API through perl. below is the unix curl command which does exactly the same thing.

I noticed that REST::Client can be used to send the REST requests from Perl. post method syntax is : POST ( $url, $body_content, %$headers )

could some one help me figure out how to populate below required fields based on the POST syntax which was described above??

curl -X POST -H "Content-Type: multipart/form-data" -H "ABC_APP_ID: <Application_ID>" -H "IABC_APP_PASSWORD: <Application_Password>" -F "deliverable_file=@sample.xml" -F "data_transaction_code=SOA" -F "file_name=RRR-xxxxxxxxxx.mmddyyyy-hhmmss.ext" -F "agency_task_order_parameter=12345667890" -F "contractor_service_request_number=123456780123456789" -F "deliverable_type=Notification" -F "data_transaction_file_date=2016-01-21" -F "tags=["tagSample1", "tagSample2"]" "http://<server>:<port>/cdx/abc/gsa/deliverables"

Replies are listed 'Best First'.
Re: how to post an xml using REST::Client
by spazm (Monk) on Apr 07, 2016 at 21:41 UTC
    Note, you'll want to learn the <code> tag when posting code on perlmonks.

    So you want to translate this curl command into Rest::Client code, right?

    curl -X POST \ -H "Content-Type: multipart/form-data" \ -H "ABC_APP_ID: <Application_ID>" \ -H "IABC_APP_PASSWORD: <Application_Password>" \ -F "deliverable_file=@sample.xml" \ -F "data_transaction_code=SOA" \ -F "file_name=RRR-xxxxxxxxxx.mmddyyyy-hhmmss.ext" \ -F "agency_task_order_parameter=12345667890" \ -F "contractor_service_request_number=123456780123456789" \ -F "deliverable_type=Notification" \ -F "data_transaction_file_date=2016-01-21" \ -F "tags=\"tagSample1\", \"tagSample2\"" \ "http://<server>:<port>/cdx/abc/gsa/deliverables"
    should look something like:
    #!/usr/bin/perl use strict; use warnings; use REST::Client; my $server = 'localhost'; my $port = '7999'; my $path = '/cdx/abc/gsa/deliverables'; my $application_id = '...'; my $application_password = '...'; my %fields = ( deliverable_file => "sample.xml", data_transaction_code => "SOA", file_name => "RRR-xxxxxxxxxx.mmddyyyy-hhmmss +.ext", agency_task_order_parameter => "12345667890", contractor_service_request_number => "123456780123456789" , deliverable_type => "Notification", data_transaction_file_date => "2016-01-21", tags => ["tagSample1", "tagSample2"] ); my $url = "http://$server:$port/cdx/abc/gsa/deliverables"; my $client = REST::Client->new({host => $url}); # add the headers '-H' lines $client->addHeader('ABC_APP_ID', $application_id); $client->addHeader('IABC_APP_PASSWORD', $application_password); # add the fields '-F' lines my $get_query = $client->buildQuery(%fields); #this doesn't help for +POST $client->POST($url);
    Note: edited to remove the major code errors

      Thanks for the tip on <code> tag. i am fairly new to the perl monks. You are correct i would need to translate the curl command into REST::Client code.

      my %fields = ( deliverable_file => "@sample.xml",
      This will attempt to interpolate the string since it is in double quotes. If you really have a file called @sample.xml (which seems like an unfortunate choice of filename), you should quote it with single quotes so Perl doesn't try to replace @sample with the contents of a non-existent array.

      Other errors with this code:

      $ perl -c 1159843.pl Possible unintended interpolation of @sample in string at 1159843.pl l +ine 11. Global symbol "@sample" requires explicit package name at 1159843.pl l +ine 11. Global symbol "$host" requires explicit package name at 1159843.pl lin +e 21. Bareword "tags" not allowed while "strict subs" in use at 1159843.pl l +ine 10. 1159843.pl had compilation errors.

      Hope this helps!


      The way forward always starts with a minimal test.

        Hi 1nickt,

        My filename is sample.xml, since the curl command has downloadable_file as @sample.xml, it was formatted like that by the previous Perl monks user who responded to my post. if i specify the downloadable_file => 'sample.xml', does it transfer the file RESTfully to the destination.

      Thank you very much for the quick reply. I will give it a try and get back to you. one question on deliverablefile, So it takes the file from current directory and transfers it over to http://$host:$port/cdx/abc/gsa/deliverables ??

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1159843]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found