使用Google Apps脚本发布JSON数据 [英] Using Google Apps Script to Post JSON Data

查看:114
本文介绍了使用Google Apps脚本发布JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Server response  :  HTTP Status 415 - Unsupported Media Type

我试图将JSON数据发布到谷歌脚本的URL,但得到上述错误。
这里是我的代码:

I am trying to post JSON data to the URL from google script but getting the above error. Here is my code:

function myFunctionpost() {
  var url = "http://abc.xyz.org/jira/rest/api/2/issue";
  var data = {"project":{ "key": "KEY"},"summary": "create issue.", "description":                 "Creating of an issue from google spreadsheet using the REST API", "issuetype": {"name": "Bug"}}  ;
   var payload = JSON.stringify(data);

   var headers = { "Accept":"application/json", 
              "Content-Type":"application/json", 
              "Authorization":"Basic _authcode_"
             };

   var options = { "method":"POST",
                "headers": headers,
                "payload" : payload
               };
   var response = UrlFetchApp.fetch(url, options);
 Logger.log(response);  

 }

我试着改变内容类型,但没有工作。authcode正在工作,因为我能够从URL中获取。

I tried changing the content-type but didn’t work .The authcode is working because I am able to GET from the URL.

任何人都知道我要去哪里错了?

Anybody has any idea about where I am going wrong? Thanks.

推荐答案

在你的options对象中加入一个contentType如下:

Put in your options object a contentType like this:

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

ContentType是fetch方法接受的高级参数之一。请参阅此处

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

这篇关于使用Google Apps脚本发布JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆