Using Google Apps Script to Post JSON Data

Add to your options a contentType object like this:

var options = {
  "method": "POST",
  "contentType": "application/json",
  "headers": headers,
  "payload": payload
};

ContentType is one of the advanced parameters that the fetch method accepts. See more here.


It is pretty counter intuitive in UrlFetchApp syntax but this:

POST /api/ra/v1/ping HTTP/1.0
Host: app.kigo.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json

Translates nicely to this curl:

curl https://app.kigo.net/api/ra/v1/ping -X POST -H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=" -H "Content-Type: application/json"

Translates to this in Google App Script:

function myFunction() {
  var headers = {
    "Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
  };
  var options = {
    "contentType": "application/json",
    "method": "post",
    "headers": headers,
    "payload": "test"
  };
  var response = UrlFetchApp.fetch("https://app.kigo.net/api/ra/v1/ping", options);
}