Table API Perl examples

Examples that demonstrate how to use the Table API with the Perl language.

Example URLs

In the examples, replace myinstance.service-now.com with the URL of your instance.

GET (Specific Record)

 #!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 my $sys_id = '0818562ca8d31100a92e8545569edcb0';
 
 my $client = REST::Client->new(host => $host);
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 # Get the incident with sys_id declared above
 $client->GET("/api/now/table/incident/$sys_id",
              {'Authorization' => "Basic $encoded_auth",
               'Accept' => 'application/json'});
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Response status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

GET (All Records in Table)

 #!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 
 my $client = REST::Client->new(host => $host);
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 # Get ALL incidents
 $client->GET('/api/now/table/incident',
              {'Authorization' => "Basic $encoded_auth",
               'Accept' => 'application/json'});
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Response status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

POST

 #!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~makamaka/JSON/lib/JSON.pm
 # Example install using cpanm:
 #   sudo cpanm -i JSON
 use JSON;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 
 my %request_body_map = (
   'active' => 'false',
   'business_stc' => '13203',
   'calendar_stc' => '86415',
   'category' => 'network',
   'description' => 'User cannot access email on mail.company.com.',
   'escalation' => '0',
   'impact' => '1',
   'incident_state' => '7',
   'knowledge' => 'false',
   'made_sla' => 'false',
   'notify' => '1',
   'number' => 'INC0000001',
   'priority' => '2',
   'severity' => '1',
   'short_description' => 'Cannot read email',
   'state' => '7',
   'sys_class_name' => 'incident',
   'sys_created_by' => 'pat',
   'sys_mod_count' => '19',
   'sys_updated_by' => 'glide.maint',
   'urgency' => '1',
 );
 
 my $client = REST::Client->new(host => $host);
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 # POST to the incident table
 $client->POST('/api/now/table/incident',
               encode_json(\%request_body_map),
               {'Authorization' => "Basic $encoded_auth",
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'}); 
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Response status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

PUT

#!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~makamaka/JSON/lib/JSON.pm
 # Example install using cpanm:
 #   sudo cpanm -i JSON
 use JSON;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 my $sys_id = '447547e0a8571100a92e8545569edc90';
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 my $client = REST::Client->new(host => $host);
 
 # First GET the record
 $client->GET("/api/now/table/incident/$sys_id",
              {'Authorization' => "Basic $encoded_auth",
               'Accept' => 'application/json'}); 
 
 if ($client->responseCode() ne '200') {
   print 'Response: ' . $client->responseContent() . "\n";
   print 'Status: ' . $client->responseCode() . "\n";
   foreach ( $client->responseHeaders() ) {
     print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
   }
   die('GET request failed');
 }
 
 # decode the response
 my $result_hash = decode_json($client->responseContent());
 my $incident_details = ${$result_hash}{'result'};
 
 # Update the incident record --- Let's prepend 'Updated via REST! ' to the short_description field
 $incident_details->{'short_description'} = 'Updated via REST! ' . $incident_details->{'short_description'};
 
 # re-encode the incident as JSON, and PUT the entire record to update it
 $client->PUT("/api/now/table/incident/$sys_id", encode_json($incident_details),
              {'Authorization' => "Basic $encoded_auth",
               'Content-Type' => 'application/json',
               'Accept' => 'application/json'});
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

PATCH

 #!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~makamaka/JSON/lib/JSON.pm
 # Example install using cpanm:
 #   sudo cpanm -i JSON
 use JSON;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 my $sys_id = '0818562ca8d31100a92e8545569edcb0';
 
 my %request_body_map = (
   'short_description' => 'New short description'
 );
 
 my $client = REST::Client->new(host => $host);
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 # PATCH the record with just the field(s) you want to update
 $client->PATCH("/api/now/table/incident/$sys_id",
                encode_json(\%request_body_map),
                {'Authorization' => "Basic $encoded_auth",
                 'Content-Type' => 'application/json',
                 'Accept' => 'application/json'}); 
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Response status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

DELETE

#!/usr/bin/env perl -w
 use strict;
 use warnings;
 use MIME::Base64;
 
 # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
 # Example install using cpanm:
 #   sudo cpanm -i REST::Client
 use REST::Client;
 
 # Set the request parameters
 my $host = 'https://myinstance.service-now.com';
 my $user = 'admin';
 my $pwd = 'admin';
 my $sys_id = 'a5ce3620a8571100a92e8545569edc99';
 
 my $client = REST::Client->new(host => $host);
 
 my $encoded_auth = encode_base64("$user:$pwd", '');
 
 # Delete the incident with sys_id declared above
 $client->DELETE("/api/now/table/incident/$sys_id",
                 {'Authorization' => "Basic $encoded_auth",
                  'Accept' => 'application/json'});
 
 print 'Response: ' . $client->responseContent() . "\n";
 print 'Status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }