【问题标题】:Google apps script urlfetch encoding URLGoogle 应用脚本 urlfetch 编码 URL
【发布时间】:2012-08-29 17:59:23
【问题描述】:
我想使用 urlfetch 来使用页面数据填充电子表格,但我尝试使用的 URL 返回一个错误作为无效参数。我认为问题在于我在 URL 中使用了被误解的字符(例如引号和括号)。
我尝试使用以下命令对 URL 进行编码,但我假设我对某些字符进行了双重编码,这会导致问题。
var encodedURL = encodeURIComponent(pageURL)
【问题讨论】:
标签:
google-apps-script
urlfetch
【解决方案1】:
尝试使用
baseURL + encodeURIComponent(parameterString)
您将传递给要查询的基本 URL 的参数作为传递给 encodeURIComponent 函数的值包含在其中。这篇文章可能对你有用:
Encode URL in JavaScript?
如果您对整个 URL 进行编码,就像您在上面所做的那样,您所编码的不仅仅是参数,我认为这就是您的问题所在。
【解决方案2】:
您个人可能不需要答案,但我为需要的人写信。
为了对 GitLab 进行成功的 API 调用(API v4),您只需对“变量”进行编码。见下文:
var url = baseUrl + projectId + "/repository/files/" + encodeURIComponent(pathAndFileName) + "?branch=" + encodeURIComponent(branch) + "&author_email=" + encodeURIComponent(authorEmail) + "&author_name=" + encodeURIComponent(authorName) + "&content=" + encodeURIComponent(content) + "&commit_message=" + encodeURIComponent(commitMessage);
其中pathAndFileName 是之前定义的变量。