Using Google Apps Script to Post JSON Data

前端 未结 2 2110

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:42

    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.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题