Powershell原生支援的cURL

來源:互聯網
上載者:User

       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》一書。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.