cancel
Showing results for 
Search instead for 
Did you mean: 
bbennett2
Navigator II

POST request with JSON data from Google Apps Script

Jump to solution

I'm working with the Canvas API and Google Apps Script (JS based). There are some examples of using Apps Script from InstructureCon 2016, but many of those were GET requests rather than POST. I also found this thread, but it hasn't resolved my issue.

I'm playing with creating a Page just because it's easy. I've tried several structures based on the suggestions I've found, none of which have worked. Using:

{
  wiki_page: {
      title: "New Page",
      body:"A new page"
   }
}‍‍‍‍‍‍

returns a 500 error. Moving wiki_page to the top level returns a 422 error. I've also tried setting the contentLength property, which also fails. URL encoding, however, works.

Is there a way to post to the API using JSON objects as payloads rather than URL encoding a string? If so, what am I overlooking?

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
bbennett2
Navigator II

For anyone having a similar issue, I've figured it out.

To post data to external APIs, Google Apps Script uses 

UrlFetchApp.fetch(url, options)

Where the URL is a string and options is an object with several key-value tuples. The apps script syntax is non-standard and apparently doesn't reformat to meet the HTTP 1.1 encoding standard. I found https://httpbin.org/ which responds to requests by sending the request string back for logging. The error, from Apps Script in particular, had to do with the Content Type header being malformed.

Apps Script expects:

{
  Authorization: Bearer <TOKEN>,
  contentType: 'application/json'
}

and the Canvas API rejects the call. Changing the apps script object to:

{
  Authorization: Bearer <TOKEN>,
  Content-Type: 'application/json'
}

resulted in a successful request.

View solution in original post

3 Replies
ColinMurtaugh
Adventurer III

Hi Brian --

Yes, you can send JSON in the POST body -- you do need to make sure that the Content-Type header is set to 'application/json' in order for Canvas to interpret it properly. 

You'll also need to make sure that all strings are quoted, like this:

{
"wiki_page": {
"title": "New Page",
"body":"A new page"
}
}

Hope this helps!

--Colin

Yeah, that was a typo. I've checked and double checked all headers, added and removed content type declarations, even used spaced over tabs. I was able to post using Postman, so that narrowed it down a little bit. My assumption at this point is that Google Apps Script is doing something weird with the request because it's giving a 400 error, saying the title attribute is blank. 

I'll have to do some more digging.

bbennett2
Navigator II

For anyone having a similar issue, I've figured it out.

To post data to external APIs, Google Apps Script uses 

UrlFetchApp.fetch(url, options)

Where the URL is a string and options is an object with several key-value tuples. The apps script syntax is non-standard and apparently doesn't reformat to meet the HTTP 1.1 encoding standard. I found https://httpbin.org/ which responds to requests by sending the request string back for logging. The error, from Apps Script in particular, had to do with the Content Type header being malformed.

Apps Script expects:

{
  Authorization: Bearer <TOKEN>,
  contentType: 'application/json'
}

and the Canvas API rejects the call. Changing the apps script object to:

{
  Authorization: Bearer <TOKEN>,
  Content-Type: 'application/json'
}

resulted in a successful request.

View solution in original post