cancel
Showing results for 
Search instead for 
Did you mean: 
Community Member

API PUT request help

Hi -

I need to make an HTTP (XHR) PUT request that targets a particular module on my self hosted canvas install and mark it done. 

I see this in the API: 

PUT /api/v1/courses/:course_id/modules/:module_id/items/:id/done

Could someone show how I might write this using AJAX? 


Thank you!

Matt

0 Kudos
2 Replies
Surveyor

Matt, 

I have not done it with AJAX but if you are familiar with PHP you can use the cURL library 

$access_token ="your token access token - this is obtained using 0auth";
$headers = array('Content-Type: application/json',sprintf('Authorization: Bearer %s', $access_token));

$ch = curl_init();
   // set url
   curl_setopt($ch, CURLOPT_URL, "https://<where your canvas is located >/api/v1/courses/:course_id/modules/:module_id/items/:id/done");
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   //return the transfer as a string
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, true);

$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

Explorer

If you must do it in JavaScript/Jquery, maybe this person's blog article will help you:

Sending RESTful GET/POST/PUT/DELETE request using js jquery – Sadaf Noor 

Most of my experience with the API have been using POST requests with Ruby, and GET requests with jQuery and Python. 

Translation of a Canvas API endpoint to a PUT request seems a matter of changing the method parameter in the $.ajax() routine to 'PUT' instead of 'POST' . The data{} parameter seems to be information to go in the request header, but since the parameters seem to be all in the URL, you might try experimenting with a Chrome browser extension called Postman:

Installing the Postman Chrome App 

Hard-code your URL with the appropriate ids and see if it manipulates your Module in the way you might expect. 

HTH!