Degraded AWS performance is currently impacting some Canvas users in the North American region. Check Canvas Status for updates.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
MatthewFallon
Community Member

Different course_id response when using curl vs. nodejs

Jump to solution

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

Accepted Solutions
MatthewFallon
Community Member

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

3 Replies
MatthewFallon
Community Member

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

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.