Showing results for 
Search instead for 
Did you mean: 
Learner II

Perl upload an SIS import file

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() );

2 Replies

Impelmenting API calls using Perl network libraries is pretty easy, since there's nothing special/implicit about cURL requests that any other library can't do.

For the authorization, you're doing Authorization: <token> but you probably meant Authorization: Bearer <token>. Other than that, you have a good start to a program that uses the Canvas API.


The token bit is fine as Christin has put the Bearer in when setting $canvasAuthToken:

     my $canvasAuthToken = 'Bearer <yourtoken>';

That is, "canvasAuthToken" is more than just the token.