我正在使用bash編寫程序,並希望使用curl來顯示網站的內容。網址是http://examplewebsite.com/example.php?pass=hello hello:world。使用:在網址中使用「:」捲曲
curl http://examplewebsite.com/example.php?pass=hello hello:world
然而,這將返回: Couldn't resolve host 'hello:world'
我正在使用bash編寫程序,並希望使用curl來顯示網站的內容。網址是http://examplewebsite.com/example.php?pass=hello hello:world。使用:在網址中使用「:」捲曲
curl http://examplewebsite.com/example.php?pass=hello hello:world
然而,這將返回: Couldn't resolve host 'hello:world'
錯誤是由「hello」和「world」之間的空格造成的,該空格導致bash將鏈接拆分爲兩個不同的標記,這兩個標記被解釋爲兩個不同的鏈接。
您至少應該引用網址參數,因爲爆炸藥片。
但是,直接傳遞參數並不是一種好的方式,因爲最終可能會有一些需要轉義的字符(空格就是其中之一,但似乎是由curl自動處理的)。
要做到這一點,你可以使用--data-urlencode
:
curl "http://examplewebsite.com/example.php" --data-urlencode "pass=hello hello:world" --get
(--data-urlencode
切換方法POST而不是GET,所以你可以使用--get
切換回GET)
謝謝,我已經設法讓它現在工作。 – Greenfrogs
我剛剛結束了'curl --data'的帖子 – Greenfrogs
「這樣直接傳遞參數不是一種好的風格。」其實這不僅是不好的風格,這只是錯誤的。 RFC 3986不允許URI中的空格。根據實施它可能會起作用,但絕對沒有保證(我相信)。 – 4ae1e1
只是包裝的URL字符串中:
curl "http://examplewebsite.com/example.php?pass=hello hello:world"
您的里程可能,這是否工作正常與否有所不同,所以您還應該對網址進行網址編碼:
curl "http://examplewebsite.com/example.php?pass=hello%22hello%3Aworld"
謝謝,但第一個例子不起作用。 – Greenfrogs
的http:// php.net/urlencode – IMSoP