Different course_id response when using curl vs. nodejs

Jump to solution
MatthewFallon
Community Explorer

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();
Labels (1)
2 Solutions
MatthewFallon
Community Explorer

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.

View solution in original post

robotcars
Community Champion

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.

View solution in original post