How to send a JSON payload with UrlFetchApp service?

  • If you set payload as a String, it will be passed directly (as a UTF-8 string).
  • If you set payload as an Object, it will be sent like an HTML form (which means either 'application/x-www-form-urlencoded' if the fields are simple, or 'multipart/form-data' if the Object includes a blob/file).

For your use case (the server is expecting to receive JSON), it sounds like Utilities.jsonStringify() is the way to go.


I do not understand the server side error but the 'payload' parameter must be a string as specified here: https://developers.google.com/apps-script/class_urlfetchapp?hl=fr-FR#fetch.

try:

var options =
{
  "method" : "post",
  "contentType" : "application/json",
  "headers" : {
    "Authorization" : "Basic <Base64 of user:password>"  
  },
  "payload" : '{ "endDate": "2012-06-03" }'
};