Google apps script urlfetch encoding URL

限于喜欢 提交于 2019-12-01 14:07:15

问题


I would like to use urlfetch to populate a spreadsheet with page data but the URL that I am trying to use is coming back with an error as an invalid argument. I think the problem is that I am using characters in the URL that are being misinterpreted (e.g. quotation marks and parentheses).

I have tried to encode the URL with the command below but I am assuming that I am double encoding some of the characters and that is causing a problem.

var encodedURL = encodeURIComponent(pageURL)

回答1:


Try using

baseURL + encodeURIComponent(parameterString)

Where you include the parameters you're passing to the base URL you are querying as the value passed to the encodeURIComponent function. This post may be of use to you:

Encode URL in JavaScript?

If you encode the entire URL, like it looks like you are doing above, you're encoding more than just the parameters, which I assume would be where your problem lies.




回答2:


You personally probably don't need the answer, but I write for the ones that need. In order to make a successful API call (API v4) to GitLab you need to encode only the "variables". See below:

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);

Where pathAndFileName is a variable that was defined earlier.



来源:https://stackoverflow.com/questions/12183817/google-apps-script-urlfetch-encoding-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!