0

I am trying to perform a POST of the action, however, when I make the request I get code 400 speaking that the action value is missing.

my code:

function mobileAPIPOST() {

  var response = UrlFetchApp.fetch(
    "https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/mobile/fakexQ_fake0Z0dKFdSblfakeTnnfakel7IYwixfake4ddZfakeAIvnmu/action", {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
        "Content-Type": "application/json",
      },
      data: {
        "action": "block"
      },
      muteHttpExceptions: true
    });

}

I updated the code following the response from @TheMaster.

My updated code:

function mobileAPIPOST() {

  var response = UrlFetchApp.fetch(
    "https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/mobile/fakexQ_fake0Z0dKFdSblfakeTnnfakel7IYwixfake4ddZfakeAIvnmu/action", {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
        "Content-Type": "application/json",
      },
      payload: {
        "action": "block"
      },
      muteHttpExceptions: true
    });

}

error pointed out:

{
  "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "parseError",
        "message": "Parse Error"
       }
      ],
      "code": 400,
      "message": "Parse Error"
     }
    }
Leticia Fatima
  • 512
  • 2
  • 4
  • 19

1 Answers1

1

UrlFetchApp doesn't have a data key parameter. Use payload instead.

Reference:

UrlFetchApp

Code Snippet:

UrlFetchApp.fetch(url,
    {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
      },
      contentType:  'application/json',
      payload: JSON.stringify({
        "action": "block"
      }),
      muteHttpExceptions: true
    });
Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85