exception - 通过 urlfetchapp 发出请求会产生“异常:属性提供的值无效:标头:内容长度”
问题描述
目标是使用 urlfetchapp 和通过 Apps 脚本的 put 请求将订单的“OrderStatusID”更新为“1”。
function updateOrderStatus(){
var private_key = "{private_key}";
var merchant_token = "{merchant_token}";
var secure_url = "{secure_url}";
var body = JSON.stringify({"OrderStatusID": "1"});
var url ="https://apirest.3dcart.com/3dCartWebAPI/v2/Orders/{orderID}";
var options = {
"method" : "put",
"headers" : {
"Content-Type" : "application/json",
"Content-Length" : body.length,
"Accept" : 'application/json',
"SecureURL" : secure_url,
"PrivateKey" : private_key,
"Token" : merchant_token
},
"body" : body,
"muteHttpExceptions" : false,
}
try{
var response = UrlFetchApp.fetch(url, options);
}
catch(err){
Logger.log(err);
}
finally{
Logger.log(response);
}
}
代码抛出错误异常:属性提供了无效值:标题:内容长度
更改代码以删除敏感信息。
解决方案
将选项“body”的名称更改为“payload”解决了这个问题。
在这个谷歌问题跟踪线程中找到了答案。
ek...@googlers.comek...@googlers.com #6Apr 11, 2016 07:03AM 状态:无法修复(不可重现) UrlFetchApp 没有名为“contentLength”的高级参数。Content-Length 标头是根据传入的有效负载的长度自动计算的。您在示例中设置的“contentLength”高级参数会被后端简单地忽略并自动计算长度。
Content-Length 是根据有效负载自动计算的。我认为它应该被命名为 body 因为3dCart API文档在示例中使用 body 作为其 json 的名称。
更正的代码:
function updateOrderStatus(){
var private_key = "{private_key}";
var merchant_token = "{merchant_token}";
var secure_url = "{secure_url}";
var body = JSON.stringify({"OrderStatusID": "1"});
var url ="https://apirest.3dcart.com/3dCartWebAPI/v2/Orders/{orderID}";
var options = {
"method" : "put",
"headers" : {
"Content-Type" : "application/json",
//"Content-Length" : body.length,
"Accept" : 'application/json',
"SecureURL" : secure_url,
"PrivateKey" : private_key,
"Token" : merchant_token
},
"payload" : body,
"muteHttpExceptions" : false,
}
try{
var response = UrlFetchApp.fetch(url, options);
}
catch(err){
Logger.log(err);
}
finally{
Logger.log(response);
}
}
推荐阅读
- r - 分配 n 个 NumericMatrix 的 Rcpp 列表
- uiimage - iOS:合并两张图片,合并重叠的内容
- android - 无法将获取的值从 firebase 数据库保存到列表
- javascript - 如何根据值验证内容可编辑的段落文本长度
- go - 在 Hugo 中使用 partial 时出现破折号
- c - 打印的边缘与节点不匹配
- lua - 在 Lua 中:如何将 HEX 值写入需要 HEX 值的应用程序?
- perl - 无法从 Microsoft Store 在适用于 Windows 10 的 Ubuntu 上安装 Bio::SeqIO 模块
- algorithm - 以2为底的整数平方根算法的证明和解释
- firebase - 错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 firebase.initializeApp()