AnsweredAssumed Answered

POST method in Java using Canvas REST API

Question asked by Ajinkya Bobade on Apr 30, 2016
Latest reply on May 7, 2016 by Ajinkya Bobade

********************I have the following Perl code:

#!/usr/bin/perl
use strict;
use JSON::Parse 'parse_json';
my $auth = "xxx";
my $curl = "curl -H 'Authorization: Bearer $auth'";
system("$curl https://canvas.instructure.com/api/v1/courses/10300000000000133 -X PUT -d 'end_at=2017-05-28T04:00:00Z' ");
my $json = '["golden", "fleece"]';
my $perl = parse_json ($json);

 

********************The function of this code is to change the "end_at" date to 2017-05-28. The output is:

{"id":10300000000000133,"name":"DOV DEV","account_id":10300000000000001,"start_at":"2014-07-01T13:31:00Z","grading_standard_id":null,"is_public":true,"course_code":"DOV DEV","default_view":"feed","enrollment_term_id":10300000000000001,"end_at":"2016-05-28T04:00:00Z","public_syllabus":false,"storage_quota_mb":15000,"is_public_to_auth_users":false,"hide_final_grades":false,"apply_assignment_group_weights":false,"calendar":{"ics":"https://canvas.instructure.com/feeds/calendars/course_Jo1suCoQU3XhYMBJNm3Ziu2POTG4GL6DjzHJ7PM4.ics"},"sis_course_id":null,"integration_id":null,"enrollments":[{"type":"teacher","role":"TeacherEnrollment","role_id":822,"user_id":10300000000020171,"enrollment_state":"active"}],"workflow_state":"available","restrict_enrollments_to_course_dates":false}host-155-246-165-53:~ ajinkyabobade$ 

 

*********************I converted the Perl code to the following Java code:

package canvas;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;


public class changedate {
public static HttpURLConnection connection;

public static void main(String[] args) throws Exception {
  
String auth = fileReader("auth.dat");
  
// put the required courseID
  
String courseId = "10300000000000133";
  
String url1 = "https://canvas.instructure.com/api/v1/courses/10300000000000133/settings";
  URL url
= new URL(url1 + courseId + "/assignments/10300000000043867/");

  connection
= (HttpURLConnection) url.openConnection();  
  connection
.setDoOutput(true);
  connection
.setRequestProperty("Authorization", "Bearer " + auth);

  connection
.setRequestMethod("PUT");
  
// OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
  
// PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));

String input = "{\n" +
" \"name\": DOV DEV,\n" +
" \"start_at\": \"2014-07-01T13:31:00Z\",\n" +
" \"grading_standard_id\": \"2013-08-28T23:59:00-06:00\",\n" +
" \"is_public\": \"true\",\n" +
" \"course_code\": \"DOV DEV\"\n"+
"}\n";
OutputStream os = connection.getOutputStream();
os
.write(input.getBytes());
os
.flush();
if (connection.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
  
+ connection.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
  
(connection.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

connection
.disconnect();

}
public static String fileReader(String fileName) throws IOException {
  
BufferedReader br = new BufferedReader(new FileReader(fileName));
  
try {
  
StringBuilder sb = new StringBuilder();
  
String line = br.readLine();

  
while (line != null) {
  sb
.append(line);
  line
= br.readLine();
  
}
  
return sb.toString();
  
} finally {
  br
.close();
  
}
}

}

 

 

*****************The purpose of the Java code is to use the Canvas REST API and POST end_date on canvas, but the code is giving error:500. How can I fix this?

Seth JohnsonJordan DaytonScott DennisJoe College@

Outcomes