Just
Just

Reputation: 437

Google Script UrlFetchApp “method” : “delete” not working

I have not used the Delete method in API before, but I tried it out several times and it doesn't affect the deletion on the system via API.

Please note that there is no error thrown while doing this

I am not sure how to go about it, cause I have tried different variations of the code and still not getting the data deleted from the system. If anyone has an idea of how to use this method, kindly help.

Here is my code.

    for (var i = 0; i<range.length; i++) {
        if(range[i][0] == searchString) {
        var lastRow = sheet.getRange(2+i,1,1,11).getValues();
        //var user_id = sheet.getRange("I2:I" + sheet.getLastRow()).getValues();
        var data ={
        'hours_per_day':lastRow[0][6],
        'starts_at':lastRow[0][7],
         'ends_at': lastRow[0][7],
         'user_id': lastRow[0][8],
         'assignable_id':lastRow[0][9],

     }

            var delete_options = {
            'method': 'DELETE',
            'Content-Type': 'application/json',

          };
var url = 'https://api.10000ft.com/api/v1/users/'+data.user_id+'/assignments/assignable_id='+ data.assignable_id+'&auth='+AUTH;
          var response = UrlFetchApp.fetch(url, delete_options);
         if (response.getResponseCode() === 200) {
          var json = JSON.parse(response);         
          sheet.getRange(2+i, 11).setValue('Pass'); 
          }
        else {
        sheet.getRange(2+i, 11).setValue('Fail');

        Logger.log(data)

        }
     }
    }
    }

Upvotes: 0

Views: 753

Answers (2)

Tanaike
Tanaike

Reputation: 201533

How about this modification?

From:

var delete_options = {
'method': 'DELETE',
'Content-Type': 'application/json',
};
var url = 'https://api.10000ft.com/api/v1/users/'+data.user_id+'/assignments/assignable_id='+ data.assignable_id+'&auth='+AUTH;
var response = UrlFetchApp.fetch(url, delete_options);

To:

var delete_options = {method: 'DELETE'};
var url = 'https://api.10000ft.com/api/v1/users/'+data.user_id+'/assignments/'+ data.assignable_id + '?auth='+AUTH;
var response = UrlFetchApp.fetch(url, delete_options);

or

var delete_options = {method: 'DELETE'};
var url = 'https://api.10000ft.com/api/v1/users/'+data.user_id+'/assignments/' + data.assignable_id + '?auth='+encodeURIComponent(AUTH);
var response = UrlFetchApp.fetch(url, delete_options);

Note:

  • When sample curl command of curl -XDELETE 'https://vnext.10000ft.com/api/v1/users/<user_id>/assignments/<assignment_id>?auth=...' is converted to Google Apps Script, it becomes above modification.
  • In your script, assignments/assignable_id='+ data.assignable_id+'&auth='+AUTH is used. But at the official document, assignments/<assignment_id>?auth=... is used. So I modified like above. But from your replying comment, I confirmed I have tried this also on postman and works okay.. So if above modification didn't resolve the issue, please test to modify from assignments/<assignment_id>?auth=... to assignments/assignable_id='+ data.assignable_id+'&auth='+AUTH.

Reference:

If this didn't resolve your issue, I apologize.

Upvotes: 1

ale13
ale13

Reputation: 6072

The delete_options parameters of your request might not be the expected ones.

Also, according to the 10,000 ft API documentation, the DELETE request looks like this:

DELETE /api/v1/users/<user_id>/assignments/<assignment_id>

Therefore, if you have a token required for your desired operation, you could include it in your options, something similar to this:

var delete_options = {
        'method': 'delete',
        'contentType': 'application/json',
        'muteHttpExceptions': true,
        'headers': {
           'Authorization' : 'bearer'+TOKEN;
           'Accept' : 'application/json'
        }
};

Upvotes: 2

Related Questions