Correct syntax for Google API PATCH request using UrlFetchApp for HTTPS Request

1.4k views Asked by At

I'm trying to use UrlFetchApp.fetch(url) method in Apps Script to PATCH a groups resource using the Google Groups Settings API.

The code below allows me to GET the groups properties, but I'm unable to figure out the syntax for a PATCH request.

function doSomething (accessToken) {

  var options = {
    method: "GET",
    headers: {
      authorization: "Bearer " + accessToken
    },
  };

  var result = UrlFetchApp.fetch("https://www.googleapis.com/groups/v1/groups/[email protected]", options);

  return HtmlService.createHtmlOutput (result.getContentText());
}
1

There are 1 answers

0
Alan Wells On BEST ANSWER

A PATCH request needs a Header Override. You actually need to use a PUT request, and then override it to a PATCH request.

var payload = "{\"" + PropertyOne + "\":\"" + "Proptery Value" + "\"}";

Logger.log('payload: ' + payload);

var options = {"method" : "put", "headers": {"X-HTTP-Method-Override": "PATCH"}, "payload" : payload};

if (payload.length > 2) {
  UrlFetchApp.fetch("https://www.googleapis.com/groups/v1/groups/[email protected]", options );
};

The code above won't be exactly what you want, and might not be error free, but the structure of it should be what you need. I'm sure the payload isn't configured correctly, because I don't know what the format is. It looks like the documentation calls it Patch body with an object.

Google Documentation - Group Settings API Patch

Key words: "Apps Script", patch