使用UrlFetchApp的HTTP请求和XML有效载荷。

问题描述 投票:1回答:1

有没有办法用Google Apps脚本提交XML有效载荷?URLFetchApp?

我已经尝试将XML文档添加到有效载荷中了

var url = "https://stackoverflow.com"; //not real
var xml = ///some xml

var params = {'content-type': 'application/xml',
              'method': 'post',
              'payload': xml};

var response = UrlFetchApp.fetch(url, params);

我期望提交请求,但在 "fetch "时出错。

我收到的错误是:"Attribute provided with invalid value: payload (line 73, file "Code")"。

"Attribute provided with invalid value: payload (line 73, file "Code")"。

google-apps-script urlfetch
1个回答
3
投票

遇到的问题

你正在传递一个无效的第二个参数给 fetch() 方法调用。

解决办法

你可以在配置对象中传递一个封闭的可接受参数集。请确保参数的名称 与参考文献中的 (见下文)。在你的情况下: content-type 应改名为 contentType (目前,您的请求将您的XML文档以 application/x-www-form-urlencoded).

考虑使用 muteHttpExceptions 设为 true 以方便以后的调试过程。

更新

经过更彻底的调查,你所遇到的错误很可能与通过一个 经解析 XML文档(如通过 XmlService)至 payload 如果你正在这样做的话(在这种情况下,错误一直在重现)。请注意 payload 可以接受 Stringbyte[]Blob 实例或 Object (视情况而定) contentType 参数)。)

修改

按照Tanaike的意见和建议,如果你确实是想通过一个。Document 实例到 payload,问题将通过准备XML来修正(注意这些操作不会突变 xml 值),例如。

  1. XmlService.getPrettyFormat().format(XmlService.parse(xml));
  2. XmlService.getRawFormat().format(xml) (如果XML已经被解析)。

有用的链接

  1. fetch() 办法 参考;
  2. XmlService 阶层 参考;
  3. getRawFormat() 办法 参考 (以备不时之需)。
© www.soinside.com 2019 - 2024. All rights reserved.