[ARCHIVED] Receive error Code 422 Unprocessable Entity using API in Javascript

Jump to solution
tadm9280
Community Novice

Hi,

I am trying to call Canvas api to update on gradebook custom column from javascript.  There's no problem when I use GET to retrieve information from gb API, but I ran into ' 422 Unprocessable Entity' when calling PUT or DELETE to update. I tried to convert to JSON format to pass in data during PUT call, still resulted in same status code.  Can anyone throw me idea what the error be possible caused?   Thanks!

Ruiling

>>>

function updateCustomfield(courseID, customID) {
   var formdata = new FormData();
   formdata.append("column[hidden]", "true");
   const requestOptions = {
                method: 'PUT',
                credentials : 'same-origin',

                body: formdata
   };

  putUrl =  "https://" +window.location.hostname +"/api/v1/courses/" + courseID + '/custom_gradebook_columns/' + customID;
    fetch(putUrl, requestOptions)
                .then(function(response) {
             console.log(response.status);
             console.log(response.text());
    });

}

>>>>

Labels (2)
2 Solutions
kona
Community Coach
Community Coach

 @tadm9280 , greetings! To help with this question, I'm sharing your question with the Canvas Developers group in Canvas. Hopefully, they'll be able to help!

Kona 

View solution in original post

0 Likes
James
Community Champion

The 422 is that Canvas couldn't make sense out of your request. At first glance (without testing or looking at the documentation for other issues), I see that you are not sending the header to accept content-type: application/json.That is necessary if you use JSON. For PUT, I've had better success if I make it part of the query string and don't mess with the body.

putUrl = "https://" +window.location.hostname +"/api/v1/courses/" + courseID + '/custom_gradebook_columns/' + customID + '?column[hidden]=true';

The second potential issue would be getting the API token in there. In GET calls, I've been able to use the browser's cookies as credentials, but it seems every time I want to to POST or PUT, I need to look up the x-csrf cookie and include that, but I might be remembering non-API calls.

Sorry for the non-certainty in the answer. I've got a lot of things I'm trying to manage right now.

View solution in original post