Community

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ankur_gupta
New Member

unable to create assignment from canvas api

Jump to solution

i am trying the following code for creating new assignment from canvas api. Can anyone help me and suggest where i am going wrong.

i am continuously getting error array as a result;

$assignment['name'] = "Assignment1";

$assignment['submission_type'][] = "none";

$assignment['points_possible'] = "100";

$assignment['grading_type'] = "points";

$assignment['muted'] = true;

$assignment['published'] = false;

$domain = "canvas.instructure.com";

$token = "GENERATED TOKEN";

$query = "/api/v1/courses/1016025/assignments";

$result = executeQuery("POST",$query,$assignemnt)

function executeQuery($method,$query,$data)

{

     global $domain;

     global $token;

     $vals = 3;

      $ch = curl_init('https://'.$domain.$query.'?access_token='.$token);

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

       curl_setopt($ch, CURLOPT_HEADER, 0);

       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

       $response = curl_exec($ch);

       return json_decode($response,true);

}

Tags (2)
1 Solution

Accepted Solutions
James
Community Champion

 @ankur_gupta ​,

In your executeQuery() call, you misspelled $assignment as $assignemnt, so it's passing an undefined variable. You're also missing a semi-colon at the end of this line.

$result = executeQuery("POST",$query,$assignemnt)  // wrong

$result = executeQuery("POST",$query,$assignment);  // correct

It's 'submission_types' plural not 'submission_type' singular:

$assignment['submission_type'][] = "none";    // wrong

$assignment['submission_types'][] = "none";    // correct

The biggest problem, though, is that your structure is not setup properly. You need to have a field called "assignment[name]". What you have is a variable called $assignment that contains a field called 'name'. When you pass that into your POST, it's simply an array with names of 'name', 'submission_type[]', 'points_possible', etc. But you need it to have names of 'assignment[name]', 'assignment[submission_types][]', and so on

The quickest way to fix that is to take your $assignment variable and put it into an array (call it $data) and then pass the $data to the executeQuery() rather than passing $assignment.

$data = array ('assignment' => $assignment);

$result = executeQuery("POST",$query,$data);

After all that, I'm getting an array to string conversion error on your CURLOPT_POSTFIELDS line. You need to use http_build_query($data) to convert it into a string. However, this encodes the key of the array into the query request, so instead of getting 'assignment[submission_types][]=none' you get 'assignment[submission_types][0]=none' and it fails. I had to add a preg_replace to remove numeric keys from the data;

$payload = preg_replace('/%5B\d+%5D/', '[]', http_build_query($data));

curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );

Then it successfully created the assignment.

There are some other areas where the code could be improved -- like not passing the access_token on the command line but using a header instead and using curlopt_post=true instead of curlopt_custommethod -- but I've tried to only include the stuff necessary to get the assignment created.

View solution in original post

4 Replies
Stef_retired
Community Team
Community Team

 @ankur_gupta , I've shared your highly technical question with the Canvas Developers​ and Canvas Admins​ groups to enhance its visibility among Community experts in API.

akkaufmann
Community Contributor

Hi Ankur,

What is the error you are receiving in the error response?

Alex

I am getting "400 Bad Request" as a response.

Also i tried sending data from header and it worked for me. But when i am using it as POSTFIELDS as described in my question then it is not working.

James
Community Champion

 @ankur_gupta ​,

In your executeQuery() call, you misspelled $assignment as $assignemnt, so it's passing an undefined variable. You're also missing a semi-colon at the end of this line.

$result = executeQuery("POST",$query,$assignemnt)  // wrong

$result = executeQuery("POST",$query,$assignment);  // correct

It's 'submission_types' plural not 'submission_type' singular:

$assignment['submission_type'][] = "none";    // wrong

$assignment['submission_types'][] = "none";    // correct

The biggest problem, though, is that your structure is not setup properly. You need to have a field called "assignment[name]". What you have is a variable called $assignment that contains a field called 'name'. When you pass that into your POST, it's simply an array with names of 'name', 'submission_type[]', 'points_possible', etc. But you need it to have names of 'assignment[name]', 'assignment[submission_types][]', and so on

The quickest way to fix that is to take your $assignment variable and put it into an array (call it $data) and then pass the $data to the executeQuery() rather than passing $assignment.

$data = array ('assignment' => $assignment);

$result = executeQuery("POST",$query,$data);

After all that, I'm getting an array to string conversion error on your CURLOPT_POSTFIELDS line. You need to use http_build_query($data) to convert it into a string. However, this encodes the key of the array into the query request, so instead of getting 'assignment[submission_types][]=none' you get 'assignment[submission_types][0]=none' and it fails. I had to add a preg_replace to remove numeric keys from the data;

$payload = preg_replace('/%5B\d+%5D/', '[]', http_build_query($data));

curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );

Then it successfully created the assignment.

There are some other areas where the code could be improved -- like not passing the access_token on the command line but using a header instead and using curlopt_post=true instead of curlopt_custommethod -- but I've tried to only include the stuff necessary to get the assignment created.