Uploading rubric from CSV sheet

Jump to solution
amehl
Community Member

Hello everyone, hope your day is going great so far.

I have a question about the Canvas API. I have used the API for different scripts and it has been working fine but now I cannot seem to figure out how to update or create a rubric on Canvas through the API.

I am able to change the name of the rubric but I cannot figure out how to actually add the criteria to it. I tried to do it like the Canvas API description specifies (or at least how I understand it) but I must be doing something wrong. 

As example, the JSON I want to send over as "data" for the API call.

{
    "id": "rubric_id",
    "rubric_association_id": "course_association_id",
    "rubric[title]": "My Rubric 4",
    "rubric[criteria]": {
        "1": {
            "description": "BBBB",
            "points": 7,
            "ratings": [
                {
                    "description": "Fuller",
                    "long_description": "",
                    "points": 7
                },
                {
                    "description": "Nope",
                    "long_description": "",
                    "points": 0
                }
            ]
        }
    }
}

This works as long as I do not include the "rubric[criteria]". So the name change works, so I must be doing something right. I tried replacing the "1" with: 0, 1, "0", "one", "zero". I also tried to replace the whole "rubric[criteria]" with a JSONArray instead of the hash list, just because I can. It always ends up giving me a "internal server error", this error always seems to happen if it is unhappy with the JSON.

This is my call with the http being the api url and as mentioned it works if I only try to change the name, so I guess this is ok and just my JSON is off. 

res = requests.put(http +'courses/'+str(id)+'/rubrics/'+str(rubric_id), data= x, headers = header)

 

If anyone has an idea what is wrong in my JSON I would really appreciate it. I am at a loss on what else to try.

Thank you very much!

1 Solution

@amehl 

It looks like the last piece you are missing is to convert 'x' into a JSON string. 

To do this in Python you will need to import json at the top of your code file.

Then, in your code you will need to use json.dumps(x) to convert 'x' into a JSON string. json.dumps() in Python 

id= 72360000000 # id taken from the IP after finding the course

# trying to build the JSON
x = {
  "rubric_association": {
    "association_type": "Course",
    "association_id": id
  },
  "rubric": {
    "title": "My Rubric 7",
    "criteria": {
      "1": {
        "description": "BBBB",
        "ratings": {
          "1": {
            "description": "Fuller",
            "long_description": "",
            "points": 7
          },
          "2": {
            "description": "Nope",
            "long_description": "",
            "points": 0
          }
        }
      }
    }
  }
}

formattedData = json.dumps(x)

header = {
  'Authorization': 'Bearer ' + c_token,
  'Content-Type': 'application/json'
}

http = "https://canvas.instructure.com/api/v1/"

# the get request works if I put it in like this, so it seems to be an ok header and id
# res = requests.get(http +'courses/'+str(id)+'/rubrics/', headers = header) 
res = requests.post(http +'courses/'+str(id)+'/rubrics/', data= formattedData, headers = header)

 

View solution in original post

0 Likes