CURL 不适用于参数中带有花括号的 URL

CURL does not work with URLs with curly braces in parameters

有些带括号的 URL 不适用于 CURL,但适用于 Chrome 和 Firefox。

例如这个 URL: https://rdtrkr.com/mg.php?voluum_id=d51b17bc-c537-4f3e-9879-2e373341ae5a&widget_id={widget_id}&campaign_id={campaign_id}&teaser_id={teaser_id}&geo={geo}&img=guy18.jpg&txt=german&lp=de&click_price={click_price}&click_id={click_id}&{click_id} 在 Chrome 和 firefox 中确实有效,但是当用 CURL 调用时,会出现 404 错误。

curl  \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36" \
-v "https://rdtrkr.com/mg.php?voluum_id=d51b17bc-c537-4f3e-9879-2e373341ae5a&widget_id={widget_id}&campaign_id={campaign_id}&teaser_id={teaser_id}&geo={geo}&img=guy18.jpg&txt=german&lp=de&click_price={click_price}&click_id={click_id}&{click_id}"

产生结果:

< HTTP/2 404 
< server: nginx
< date: Thu, 13 Dec 2018 16:53:45 GMT
< content-type: text/html; charset=UTF-8
< content-length: 0

但是在 "Preserve log" 模式下使用 chrome 开发者工具,我有:

CURL 收到 404 而不是 302 重定向。它与 CURL 可能是 URL 编码括号有关吗?我不知道这里出了什么问题。

ps:我不是我在示例中使用的网站的所有者。

大括号为unsafe in URLs。 cURL(不同于Google Chrome)试图帮你一个忙并自动编码URL.

换句话说,它将 { 转换为 %7B,将 } 转换为 &7D

为了防止这种行为,您可以使用 -d 传递查询字符串参数。 因为 -d 将请求更改为 POST,您还需要使用 -G 强制请求为 GET。

所以不要做

curl "http://example.com?param1=xxx&param2=yyy"

你可以做到

curl "http://example.com" -G -d "param1=xxx&param2=yyy"

在您的特定情况下,出于某种原因,您所定位的网络服务器仍将 return 404,除非您提供 Accept-Language header:

curl -v "http://rdtrkr.com/mg.php" \
     -G -d "voluum_id=d51b17bc-c537-4f3e-9879-2e373341ae5a&widget_id={widget_id}&campaign_id={campaign_id}&teaser_id={teaser_id}&geo={geo}&img=guy18.jpg&txt=german&lp=de&click_price={click_price}&click_id={click_id}&{click_id}" \
     -H "Accept-Language: en-US,en;q=0.9,fr;q=0.8,ru;q=0.7,es;q=0.6"

给予

*   Trying 34.192.193.118...
* Connected to rdtrkr.com (34.192.193.118) port 80 (#0)
> GET /mg.php?voluum_id=d51b17bc-c537-4f3e-9879-2e373341ae5a&widget_id={widget_id}&campaign_id={campaign_id}&teaser_id={teaser_id}&geo={geo}&img=guy18.jpg&txt=german&lp=de&click_price={click_price}&click_id={click_id}&{click_id} HTTP/1.1
> Host: rdtrkr.com
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-Language: en-US,en;q=0.9,fr;q=0.8,ru;q=0.7,es;q=0.6
>
< HTTP/1.1 302 Found
< Server: nginx
< Date: Thu, 13 Dec 2018 17:39:18 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 0
< Connection: keep-alive
< Location: https://rotronica-premarity.com/d51b17bc-c537-4f3e-9879-2e373341ae5a?widget_id={widget_id}&campaign_id={campaign_id}&teaser_id={teaser_id}&geo={geo}&img=guy18.jpg&txt=german&lp=de&click_price={click_price}&click_id={click_id}
<
* Connection #0 to host rdtrkr.com left intact

使用这个标志(来自man curl):

-g/--globoff
              This  option  switches  off  the "URL globbing parser". When you set this option, you can
              specify URLs that contain the letters {}[] without having them being interpreted by  curl
              itself.  Note  that  these  letters  are not normal legal URL contents but they should be
              encoded according to the URI standard.