在网址中卷曲“:”

时间:2015-04-07 18:35:41

标签: bash curl

我正在用bash编写程序,并希望使用curl来显示网站的内容。网站网址是http://examplewebsite.com/example.php?pass=hello你好:世界。使用:

curl http://examplewebsite.com/example.php?pass=hello hello:world

但是这会返回: Couldn't resolve host 'hello:world'

2 个答案:

答案 0 :(得分:2)

只需将URL包装在一个字符串中:

curl "http://examplewebsite.com/example.php?pass=hello hello:world"

您的里程数可能会有所不同,因此您也应该对值进行URL编码:

curl "http://examplewebsite.com/example.php?pass=hello%22hello%3Aworld"

答案 1 :(得分:2)

错误是由“hello”和“world”之间的空间引起的,这个空间导致bash将链接分成两个不同的标记,这些标记被卷曲解释为两个不同的链接。

你应该至少引用URL参数,就像Explosion Pills那样。

然而,直接传递参数并不是一种好的风格,因为你可能会得到一些需要转义的字符(空格就是其中之一,但它似乎是由curl自动处理的。)

为此,您可以使用--data-urlencode

curl "http://examplewebsite.com/example.php" --data-urlencode "pass=hello hello:world" --get

--data-urlencode将方法切换为POST而不是GET,因此您可以使用--get切换回GET)