Christin Deville

Perl upload an SIS import file

Discussion created by Christin Deville on Apr 10, 2017
Latest reply on May 1, 2017 by Peter Love

I spent some time struggling with this before figuring it out (cURL is well documented, but translating that to Perl, not so much), so I figure I am not the only one. Critique welcome.

 

Here is a bit of Perl that will allow you to upload a csv file to your SIS Imports API. Replace <> with relevant data (don't put the "<>" in your code). This Perl is the equivalent of cURL:

 

From SIS Imports - Canvas LMS REST API Documentation 

curl -H 'Content-Type: text/csv' --data-binary @<filename>.csv \     -H "Authorization: Bearer <token>" \     'https://<canvas>/api/v1/accounts/<account_id>/sis_imports.json?import_type=instructure_csv'

Perl here:

#!/usr/bin/perl -w
#Perl program to send up files to Canvas SIS Import API

 

use strict;
use warnings;

 

 

use HTTP::Response;
use HTTP::Headers;
use HTTP::Request::Common;
use LWP::UserAgent;
use JSON qw{decode_json};

 

 

#Setup header
my $canvasAuthToken = 'Bearer <yourtoken>';
my $post_course_filename = 'test_course.csv';
my $post_course_URL = 'https://<your canvas instance>/api/v1/accounts/self/sis_imports.json?import_type=instructure_csv';

 

my $post_course_header = HTTP::Headers->new(
'Authorization' => $canvasAuthToken,
'Content-Type' => 'text/csv',
);

 

#From CPAN: $request = HTTP::Request->new( $method, $uri, $header, $content ) 
#Build and post request
my $request = HTTP::Request->new( POST =>,$post_course_URL, $post_course_header, Content => $post_course_filename );
my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);
if ( $response->is_success ) {
  print( $response->decoded_content . "\n" );
  my $json = decode_json( $response->decoded_content );
}
else {
  print( STDERR $response->status_line() );
}

Outcomes