0

When using google Apps Script, sometimes I create errors on external API's I'm using.

The response is:

Exception: Request failed for https://dev1.example.com returned code 400. Truncated server response: {"code":"rest_invalid_param","message":"Invalid parameter(s): line_items","data":{"status":400,"params":{"line_items":"line_items[0][subtotal] is n... (use muteHttpExceptions option to examine full response)

I understand how to set the muteHttpExceptions :


  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'headers': headers,  
    'muteHttpExceptions': true,
    'payload': JSON.stringify(data)
  };
  apiurl = "https://" + apidomain + "/wp-json/wc/v3/customers/"

but I don't understand where I should then be looking for the untruncated server response.

The documentation: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app is not something I'm finding very helpful

Am I looking on the API server endpoint logs?

Or somewhere in Appscript?

Maybe here?

  var response = UrlFetchApp.fetch(apiurl, options);
  Logger.log(response);

Or in my local web browser?

Or am I totally confused?

I'm new to SO and Apps Script, so please be gentle.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Tim Dobson
  • 85
  • 7

1 Answers1

0

As per this answer, the muteHttpExceptions option stops the script stopping on error, but instead puts the server error into the response of the call. This is the bit you'd usually look for your response from your API Call.

So this is how you'd get the full response, and it'd appear on the Apps Script debugging interface:

  var headers = { "Authorization": "Basic " + encodedAuthInformation };
  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'headers': headers,  // Convert the JavaScript object to a JSON string.
    'muteHttpExceptions': true,
    'payload': JSON.stringify(data),
  };
  apiurl = "https://" + apidomain + "/wp-json/wc/v3/orders/"
  var response = UrlFetchApp.fetch(apiurl, options);
  Logger.log(response);
Rubén
  • 34,714
  • 9
  • 70
  • 166
Tim Dobson
  • 85
  • 7