如何使用 bash 或 curl 将数据编码为 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37309551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:38:56  来源:igfitidea点击:

How to urlencode data into a URL, with bash or curl

bashcurlurlencode

提问by Edward Ned Harvey

How can a string be urlencoded and embedded into the URL? Please note that I am nottrying to GET or POST data, so the -Gand --dataand --data-urlencodeoptions of curl don't seem to do the job.

如何将字符串编码并嵌入到 URL 中?请注意,我不是在尝试 GET 或 POST 数据,因此curl的-Gand--data--data-urlencodeoptions 似乎无法完成这项工作。

For example, if you used

例如,如果您使用

curl -G http://example.com/foo --data-urlencode "bar=spaced data"

that would be functionally equivalent to

这在功能上等同于

curl http://example.com/foo?bar=spaced%20data"

which is not desired.

这是不希望的。

I have a string foo/barwhich must be urlencoded foo%2fbarand embedded into the URL.

我有一个字符串foo/bar,必须对其进行 urlencodedfoo%2fbar并嵌入到 URL 中。

curl http://example.com/api/projects/foo%2fbar/events

One hypothetical solution (if I could find something like this) would be to preprocess the data in bash, if there exists some kind of urlencodefunction.

一个假设的解决方案(如果我能找到类似的东西)是在 bash 中预处理数据,如果存在某种urlencode函数的话。

DATA=foo/bar
ENCODED=`urlencode $DATA`
curl http://example.com/api/projects/${ENCODED}/events

Another hypothetical solution (if I could find something like this) would be some switch in curl, similar to this:

另一个假设的解决方案(如果我能找到类似的东西)是 curl 的一些开关,类似于:

curl http://example.com/api/projects/{0}/events --string-urlencode "0=foo/bar"

The specific reason I'm looking for an answer to this question is the Gitlab API. For example, gitlab get single projectNAMESPACE/PROJECT_NAMEis URL-encoded, eg. /api/v3/projects/diaspora%2Fdiaspora(where /is represented by %2F). Further to this, you can request individual properties in the project, so you end up with a URL such as http://example.com/projects/diaspora%2Fdiaspora/events

我正在寻找这个问题的答案的具体原因是 Gitlab API。例如,gitlab get 单个项目NAMESPACE/PROJECT_NAME是 URL 编码的,例如。/api/v3/projects/diaspora%2Fdiaspora(其中/由 表示%2F)。除此之外,您可以请求项目中的各个属性,因此您最终会得到一个 URL,例如http://example.com/projects/diaspora%2Fdiaspora/events

Although this question is gitlab-specific, I imagine it's generally applicable to REST API's in general, and I'm surprised I can't find a pre-existing answer on stackoverflow or internet search.

虽然这个问题是特定于 gitlab 的,但我想它通常适用于 REST API,而且我很惊讶我无法在 stackoverflow 或互联网搜索上找到预先存在的答案。

回答by Charles Duffy

The urlencodefunction you propose is easy enough to implement:

urlencode您提出的功能很容易实现:

urlencode() {
  python -c 'import urllib, sys; print urllib.quote(sys.argv[1], sys.argv[2])' \
    "" "$urlencode_safe"
}

...used as:

...用作:

data=foo/bar
encoded=$(urlencode "$data")
curl "http://example.com/api/projects/${encoded}/events"

If you want to have some characters which are passed through literally -- in many use cases, this is desired for /s -- instead use:

如果您想要一些字面传递的字符——在许多用例中,这是/s所需要的——而是使用:

encoded=$(urlencode_safe='/' urlencode "$data")