Google App Scripts - URLFetchApp API Endpoint Requests other than GET method

Jump to solution
themidiman
Community Champion

I've given myself a project to build some API automation tools around data that is contained in Google Sheets.

I see only a smattering of similar projects on the Developers Group forum that seem to imply success without re-educating the group what their entire code is that solved their issue. Here's one that implies success but seems to be missing the entire code: https://community.canvaslms.com/t5/Canvas-Developers-Group/POST-request-with-JSON-data-from-Google-A... 

@bbennett2 would you be able to share what you did that worked?

I can use URLFetchApp all day to get data out of Canvas using GET, but when it comes to putting it back or manipulating it using POST, PUT, or DELETE, I'm running into roadblocks.

I've been trying to automate the creating of sandbox type courses for faculty, and want to use these two endpoints:

https://canvas.instructure.com/doc/api/courses.html#method.courses.create

and

https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.create 

Both use the POST method, and I'm starting with just the Enrollments endpoint for starters. Here's my code:

 

 

function enrollUser(user_id,course_id) {
  var requestURL = "<CANVAS_URL>/api/v1/courses/" + course_id + "/enrollments"
  var data = {
     'user_id': user_id
  };
  var options = {
    'muteHttpExceptions': true,
    'method': 'post',
    'Content-Type': 'application/json',
    'headers': {
      "Authorization" :"Bearer "+token,
      
      },
    
    'payload': JSON.stringify(data)
  };
  var result = UrlFetchApp.fetch(requestURL, options);
  Logger.log(result);
}

 

 

This results in 

 

{"message":"No parameters given"}

 

So my confusion is when comparing to the Live API where you just submit the required information, is all of that supposed to go in the data payload, does it go on the URL as part of the query string? 

If I can get a concrete example of what constitutes a successful POST, PUT, or DELETE request in Google Apps Scripts, I can build up more of my library of tools to use Google Workspace/Docs to automate certain aspects of my role at my institution.

Thanks in advance.

Labels (4)
0 Likes
1 Solution

@JamesSekcienski ,

Thank you 

This is the missing piece of the puzzle. The earlier forum post was misleading me all this time. The assertion was that Google's App Script Documentation for URLFetch used 'contentType' instead of 'Content-Type' as the header name, but Canvas API didn't like it that way. It appears that isn't the case. Here's my final code that actually works now (hooray!)

 

 

function enrollUser(user_id,course_id) {
  var requestURL = "<CANVAS_DOMAIN_URL>/api/v1/courses/" + course_id + "/enrollments"
  var data = {
     'enrollment': {
        'user_id': user_id,
        'type': 'TeacherEnrollment'
    }
  };
  var options = {
    'muteHttpExceptions': true,
    'method': 'post',
    'contentType': 'application/json',
    'headers': {
      "Authorization" :"Bearer "+token
      
      },
    'payload': JSON.stringify(data)
  };
  var result = UrlFetchApp.fetch(requestURL, options);
  Logger.log(result);
}

 

 

View solution in original post