The Instructure Community will enter a read-only state on November 22, 2025 as we prepare to migrate to our new Community platform in early December.
Read our blog post for more info about this change.
For a class project I am currently working with a professor to access course discussion info programmatically from canvas. To do this I started with testing using curl and then moved to nodejs. I found when making the identical https call through curl and then through nodejs (code snippets below, token removed for privacy), the curl request returned the correct id for the course (127210000000003374) while the nodejs request returned an id incremented by 6 for the course (127210000000003380). Is there something significantly different about how curl requests function from the node https library? Any help would be appreciated, I'm not asking for the answer just to know if there is a header I need to add or anything.
The working curl request (with <insert-token> subbed in)
curl -H "Authorization: Bearer <insert-token>" "https://canvas.instructure.com/api/v1/courses"
The nodeJS request made with the standard library returning wrong id value for courses (with <insert-token> subbed in)
const https = require("https");
const reqHeaders = {
"Authorization": "Bearer <insert-token"
}
const options = {
hostname: "canvas.instructure.com",
port: 443,
path: "/api/v1/courses/",
headers: reqHeaders,
method: "GET"
}
const req = https.request(options, (res) => {
let resString = ""
console.log("response status: " + res.statusCode)
res.on('data', (each) => {
resString += each;
});
res.on('end', () => {
response = JSON.parse(resString);
console.log(response);
})
});
req.end();
Solved! Go to Solution.
I diagnosed my own problem so will reply here. JSON.parse method has issues with large integers that causes it to have major rounding errors. I fixed the issue by using a special JSON library called "json-bigint" in npm.
This documented, https://canvas.instructure.com/doc/api/
To force all ids to strings add the request header Accept: application/json+canvas-string-ids This will cause Canvas to return even integer IDs as strings, preventing problems with languages (particularly JavaScript) that can't properly process large integers.
I diagnosed my own problem so will reply here. JSON.parse method has issues with large integers that causes it to have major rounding errors. I fixed the issue by using a special JSON library called "json-bigint" in npm.
This documented, https://canvas.instructure.com/doc/api/
To force all ids to strings add the request header Accept: application/json+canvas-string-ids This will cause Canvas to return even integer IDs as strings, preventing problems with languages (particularly JavaScript) that can't properly process large integers.
Thank you so much for this! This is actually a much more elegant solution to the problem I appreciate your response, Going to leave both marked as a solution only because my solution also allows for the numbers to actually be used if that was required for any reason, but this should be used under most situations.
Community helpTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.