cURL (clients for URL) 是一款常用的命令列工具,它被用於基於URL傳輸資料,它支援HTTP, HTTPS,FTP等協議。其實在Windows平台上,從Powershell 3.0 起增加了一個類似的命令Invoke-WebRequest, 執行 Get-Help Invoke-WebRequest 會看到下面的協助資訊。注意看一下其中的ALIASES部分,curl赫然在列。也就是說,你可以直接使用curl作為命令名字,呵呵!
Invoke-WebRequest的文法與cURL有所不同,但如果會用cURL,轉換到使用Invoke-WebRequest非常簡單,下面舉幾個使用cURL和Invoke-WebRequest操作Elasticsearch的例子 (cURL表示cURL.exe命令,Invoke-WebRequest則是Powershell中的的實現):
- cURL -XGET 'localhost:9200/library/book/_search'
- Invoke-WebRequest http://localhost:9200/library/book/_search -Method GET (GET操也作可以省略)
- cURL -XPOST 'localhost:9200/library'
- Invoke-WebRequest http://localhost:9200/library -Method POST
- cURL -XPUT 'localhost:9200/library/book/_mapping' -d @mapping.json
- Invoke-WebRequest http://localhost:9200/library/book/_mapping -Method PUT -InFile mapping.json
- cURL -XPOST 'localhost:9200/blog/article' -d '{"title":"Elasticsearch"}'
- Invoke-WebRequesthttp://localhost:9200/blog/article -Method POST -Body '{"title":"Elasticsearch"}'
- cURL -XGET 'localhost:9200/library/book/_search?q=title:crime&pretty=true'
- Invoke-WebRequesthttp://localhost:9200/library/book/_search?q=title:Elasticsearch"&"pretty=true -OutFile response.txt
- cURL -XPOST 'localhost:9200/library/book/_search?pretty=true' -d '{"query":{"term":{"title":"wp8.1"}}}'
- Invoke-WebRequesthttp://localhost:9200/library/book/_search?pretty=true -Method POST -Body'{"query":{"term":{"title":"wp8.1"}}}'
- cURL -XPOST 'localhost:9200/library/book/_search?pretty=true' -d'{"query":{"terms":{"tags":["surface", "wp8.1"],"minimum_match":2}}}'
- Invoke-WebRequesthttp://localhost:9200/library/book/_search?pretty=true -Method POST -Body '{"query":{"terms":{"tags":["surface", "wp8.1"],"minimum_match":2}}}'
除了Invoke-WebRequest,Powershell 3.0起還提供了Invoke-RestMethod命令,它專門用於向RESTful web服務發送HTTP和HTTPS資料。兩個命令很相似,但也有不同,具體的不同可以參見《Widnows PowerShell》一書。