-5

我有一个脚本处理一些退款付款,这些付款从大约 2月10日。服务器响应 Content-Type is not supported ... 即使我的标头 Content-Type 看起来确实正确

相同的代码在该日期之前工作,这让我相信服务器端发生了一些变化,但我最近在 square-connect API 更改日志中没有注意到任何可以解释这个问题的东西。

任何帮助将不胜感激:)谢谢

请参阅下面的 JSON 请求和响应。

{"method":"POST",
  "headers":
    {"Authorization":"Bearer [mypersonalid]",
     "Accept":"application/json",
     "Content-Type":"application/json"},
     "payload":
       {"payment_id":"[paymentid]",
        "type":"FULL",
        "reason":"reason"
       }
    }"
}


{"type":"bad_request",
 "message":"The Content-Type of this request is not supported. 
            Supported     type(s): application/json"
}
4

2 回答 2

0

由于您使用的是 Google App Script,因此您似乎必须使用特殊contentType参数(此处的文档)设置 Content-Type 标头。这样,Google Apps 脚本就知道将负载序列化为 JSON 而不是多部分表单数据。如果您只是将其指定为标头,Google 将覆盖它。

于 2015-03-04T20:12:40.057 回答
0

非常感谢您的评论。解决了!

我在参数中添加了 contentType,但它没有用,但我开始收到一个新错误,说“坏 json 对象”。Si,我刚刚搜索了一下,发现“有效负载”对象的实际编码需要是“字符串化”。请参阅 user1021710 的回答:https ://stackoverflow.com/a/18851043/4615977 (“带有 JSON 有效负载的 Google Apps 脚本 UrlFetchApp”)

所以在这种情况下, contentType 是必要的,然后是有效负载的编码。谷歌显然在 2 月中旬左右改变了他们自己的 API,因为代码是相同的并且可以工作。

谢谢各位!!!

使用谷歌应用脚​​本发布退款请求的示例基本代码:

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

var payload = {
   "payment_id" : paymentid,
   "type" : type,  
   "reason" : reason,
};

var params = {
  "contentType": "application/json",
  "headers" : headers,
  "method" : "post",
  "payload" : JSON.stringify(payload),
};

var url = "https://connect.squareup.com/v1/me/refunds";
paymentsFetch = UrlFetchApp.fetch(url, params);
于 2015-03-08T02:33:22.213 回答