首页 > 解决方案 > 在 curl 中禁用正则表达式解析

问题描述

我正在尝试卷曲一个接受正则表达式作为参数之一的 API。

我发现 curl 在这里试图变得聪明,并且正在解析正则表达式本身,而不是将其视为 URL 的一部分。因此,每次我启动 curl 命令时,它都会发出 70,000 个 HTTP 请求。它应该只做一个。

例子:https://api.example.com/v1/?regex=(123[4-5][6-7][8-9])

在这种情况下,我希望文字 HTTP 请求.

相反,我得到:

[1/8]: https://api.example.com/v1/?regex=(123468) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123468)
curl: (6) Could not resolve host: api.example.com

[2/8]: https://api.example.com/v1/?regex=(123469) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123469)
curl: (6) Could not resolve host: api.example.com

[3/8]: https://api.example.com/v1/?regex=(123478) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123478)
curl: (6) Could not resolve host: api.example.com

[4/8]: https://api.example.com/v1/?regex=(123479) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123479)
curl: (6) Could not resolve host: api.example.com

[5/8]: https://api.example.com/v1/?regex=(123568) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123568)
curl: (6) Could not resolve host: api.example.com

[6/8]: https://api.example.com/v1/?regex=(123569) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123569)
curl: (6) Could not resolve host: api.example.com

[7/8]: https://api.example.com/v1/?regex=(123578) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123578)
curl: (6) Could not resolve host: api.example.com

[8/8]: https://api.example.com/v1/?regex=(123579) --> <stdout>
--_curl_--https://api.example.com/v1/?regex=(123579)
curl: (6) Could not resolve host: api.example.com

如何禁用 curl 的正则表达式解析?

我查了一下curl --help all,没有关于这种解析的内容。我还检查了 curl 的手册页。那里也什么都没有;这似乎是一个完全没有记录的“有用”功能。

它似乎是 curl,而不是 shell,因为 wget 似乎不会发生这种情况,它只发出一个(失败的请求)。

试图转义括号\似乎会阻止解析,但也会以一种看起来不正确的方式转换字符:

root@voip:/tmp# wget "https://api.example.com/v1/?regex=(123\[4-5\]\[6-7\]\[8-9\])"
--2021-11-05 11:26:03--  https://api.example.com/v1/?regex=(123%5C[4-5%5C]%5C[6-7%5C]%5C[8-9%5C])
Resolving api.example.com (api.example.com)... failed: Name or service not known.
wget: unable to resolve host address ‘api.example.com’
root@voip:/tmp# wget "https://api.example.com/v1/?regex=(123\[4-5]\[6-7]\[8-9])"
--2021-11-05 11:26:25--  https://api.example.com/v1/?regex=(123%5C[4-5]%5C[6-7]%5C[8-9])
Resolving api.example.com (api.example.com)... failed: Name or service not known.
wget: unable to resolve host address ‘api.example.com’

标签: regexbashapicurl

解决方案


要使用 {} 和 [] 禁用 URL 序列和范围,您可以使用 option -g


推荐阅读