1

I am having trouble getting this block of code to work and I believe it to be on Google's end since the same code works flawlessly in Netbeans IDE. I am also able to PUT and POST the same data through Google script, but am not able to DELETE from it. I worry that although the documentation here states that UrlFetchApp is capable of deleting, it may not actually be implemented or working. I haven't found a single example online of someone using "method":"delete" with the URLFetchApp. Has anyone ever been able to make a delete work and if not, do you see anything with my existing code that would make a PUT and POST work and not a DELETE? Thanks!

var delete_options = {
     'method' : 'DELETE',
     'contentType': 'application/json',
     'payload' : JSON.stringify(modifySkillsForAgentsPayload),
     'headers' : {
       'Authorization' : 'bearer ' + accessToken,
       'Accept' : 'application/json'

     }
  };
  UrlFetchApp.fetch(url,delete_options);
Jerrybibo
  • 1,315
  • 1
  • 21
  • 27

2 Answers2

1

You could also try modifying your modifySkillsForAgentsPayload, encoding it with encodeURIComponent, and appending it to the URL. That worked for me!

Here's the code I ended up using:

// More information at http://apidocs.yotpo.com/reference#delete-a-purchase
function deleteYotpoOrder(order_id) {

  // Token retrieved from API
  var token = 'randomStringofLettersAndNumbers9cg078as-0345';

  var orders = encodeURIComponent('[{"order_id": "' + order_id + '"}]');

  //Set the method to GET, POST, or DELETE
  var params = {
    "method": "DELETE",
    'contentType': 'application/json'
    // Can't use paylad with "DELETE" method
    //'payload' : JSON.stringify(data),
  };

  var deleteFromYotpoURL = "https://api.yotpo.com/apps/YOUR_APP_KEY/purchases?utoken=" + token + "&orders=" + orders;

  try {
  
    var result = UrlFetchApp.fetch(deleteFromYotpoURL, params);
    var strFrmFbObj = result.getContentText();
    Logger.log("Result of Delete request: " + strFrmFbObj);

  } catch (e) {
    var errorSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Errors');
    var lastRow = errorSheet.getLastRow();
    var timestamp = new Date();
    var cell = errorSheet.getRange('A1');
    cell.offset(lastRow, 0).setValue(e.message);
    cell.offset(lastRow, 1).setValue(e.fileName);
    cell.offset(lastRow, 2).setValue(e.lineNumber);
    cell.offset(lastRow, 3).setValue(timestamp);
  }
}
Noah Jeffrey
  • 119
  • 1
  • 6
0

This code will work for you.

function test()
{
  var url = "https://jsonplaceholder.typicode.com/posts/1";

  var delete_options = {
    'method' : 'DELETE',
    'contentType': 'application/json',
    //      'payload' : JSON.stringify({"test":"test"}),
    'headers' : {
      'Authorization' : 'bearer ' + ScriptApp.getOAuthToken(),
      'Accept' : 'application/json'        
    }
  };

  var blob = UrlFetchApp.fetch(url, delete_options);
  Logger.log(blob);
}

The reason, why your code is not working because HTTP DELETE operation isn't supposed to have a request body. Many (or almost all of them) REST APIs are designed that way.

Although it is not forbidden to send request body in delete request. You can read more about it here.

Parag Jadhav
  • 1,853
  • 2
  • 24
  • 41