#!/sbcimp/run/pd/csm/64-bit/perl/5.16.1/bin/perl use strict; use warnings; use lib '/dir.../lib'; use MIME::Base64; use SOAP::Lite; use File::MimeInfo; use File::Basename; use Data::Dumper; # the ServiceNow instance my $SNC_HOST = "https://server_name:16000"; my $base64; my $buf; my $mime_type; my $fname; print "var initialised\n"; #escape certificate check $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; my $line = "/file_path..../vdpsloader4.jpg"; # upload and attach a file on the local disk, base 64 encode it into a string first #while(){ #my $line = $_; #chomp($line); $mime_type = mimetype($line); $fname = basename($line); open(SFILE, $line) or die "$!"; while (read(SFILE, $buf, 60*57)) { $base64 .= encode_base64($buf); print "reading file\n"; # call the sub routine to attach attach_incident(); } #} sub attach_incident { # target the ecc_queue table my $soap = SOAP::Lite->proxy("$SNC_HOST/ecc_queue.do?SOAP"); my $method = SOAP::Data->name('insert')->attr({xmlns => 'http://www.service-now.com/'}); # set the ecc_queue parameters my @params = (SOAP::Data->name(agent => 'AttachmentCreator')); push(@params, SOAP::Data->name(topic => 'AttachmentCreator') ); # identify the file name and its mime type push(@params, SOAP::Data->name(name => '$fname:application/$mime_type') ); # attach to the incident, specifying its sys_id push(@params, SOAP::Data->name(source => 'incident:uniqueID') ); # set the payload to be the base 64 encoded string representation of the file push(@params, SOAP::Data->name(payload => $base64) ); print "before WS invoke\n"; # invoke the web service print "++++++++++++\n" print "@params" my $result = $soap->call($method => @params); print Dumper($result); print "after WS invoke\n"; print_fault($result); print_result($result); } sub print_result { my ($result) = @_; if ($result->body && $result->body->{'insertResponse'}) { my %keyHash = %{ $result->body->{'insertResponse'} }; foreach my $k (keys %keyHash) { print "name=$k value=$keyHash{$k}\n"; } } } sub print_fault { my ($result) = @_; if ($result->fault) { print "faultcode=" . $result->fault->{'faultcode'} . "\n"; print "faultstring=" . $result->fault->{'faultstring'} . "\n"; print "detail=" . $result->fault->{'detail'} . "\n"; } } # use the itil user for basic auth credentials sub SOAP::Transport::HTTP::Client::get_basic_credentials { return 'user_name' => 'pswd'; }