Using Google Apps Script to Post JSON Data

前端 未结 2 2124

I am trying to post JSON data to the URL from google script but getting the above error:

Server response : HTTP Status 415 - Unsupported Media Type

2条回答
  •  滥情空心
    2020-12-29 10:53

    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);
    }
    

提交回复
热议问题