How do I use API to change end date in bulk for quizzes?

Jump to solution
stephen_rhea
Community Novice

I apologize if this question is very simple but I am new to api, so I am hoping to get an easy answer.

I am trying to automate an api process using Postman to update quiz[lock_at] dates for all courses within a Sub-Account. I have figured out how to do it individually but I am having difficulties defining the property to automate it and move the api from one course to the next, changing the quizzes in the subaccount. Here is what I have in "Tests" for my GET request.

// Parsing the response returned by the request.
var jsonData = pm.response.json();

// Extracting the token from the response and setting it as a global variable.
pm.globals.set("newcourse_id", jsonData.course["id"]);

The response I am getting in the Test Results is 

There was an error in evaluating the test script:  TypeError: Cannot read property 'id' of undefined

I know the issue is with my defining the Canvas Course ID, but I admit I am lost. Any assistance would be great, or if anyone already has a code/collection on how to update all quiz dates in a Sub-Account, that would be great.

Labels (1)
0 Likes
1 Solution
stephen_rhea
Community Novice
Author

Hello Brad,


Thank you for your help. After playing with it with one of our Multi-Media Designers this is how we got it to work.

pm.test("Body contains id", function(){

// Parsing the response returned by the request.
let jsonData = pm.response.json();
console.log('JSON response:', jsonData);

// Extracting the token from the response and setting it as a global variable.
pm.globals.set("newcourse_id", jsonData[0].id);
});

It looks like the jsonData was coming back as an array so we had to point it to that. Logging 'pm.response' was his suggestion too and that is how we uncovered it.

So for anyone who is curious here was the GET:

https://YOURINSTITUTION.instructure.com:CANVASINSTANCEID/api/v1/accounts/ACCOUNTID/courses?page={{current_page}}&per_page=1

Then we had too more requests. One was to Set the Quiz ID. GET:

https://YOURINSTITUTION.instructure.com:443/api/v1/courses/{{newcourse_id}}/quizzes/

With the Tests Script:

pm.test("Body contains id", function(){

// Parsing the response returned by the request.
let jsonData = pm.response.json();
console.log('JSON response:', jsonData);

// Extracting the token from the response and setting it as a global variable.
for (let i = 0; i < jsonData.length; i++) {
if (jsonData[i].title == "Final Exam") {
pm.globals.set("quiz_id", jsonData[i].id);
}
}

});

Then brought all the information together. So its three requests but a lot faster to automatically change the script. The last piece is just figuring out how to get it to run the three requests.

Thanks for all your help.

View solution in original post