Powershell原生支持的cURL - Invoke-WebRequest 及与 cURL的使用区别

2 篇文章 0 订阅
1 篇文章 0 订阅


Powershell原生支持的cURL - Invoke-WebRequest

http://www.aichengxu.com/shell/2527399.htm


在windows的powershell下,执行elasticsearch的curl的如下命令时,总提示错误,

PS C:\Users\xuewen.liang> curl -XPUT 'localhost:9200/customer?pretty'
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'XPUT'.
At line:1 char:6
+ curl -XPUT 'localhost:9200/customer?pretty'
+      ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\Users\xuewen.liang> curl -XPUT 'http://localhost:9200/customer?pretty'
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'XPUT'.
At line:1 char:6
+ curl -XPUT 'http://localhost:9200/customer?pretty'
+      ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand


原因如下,是应为要替换成power里的Invoke-WebRequest命令的相应写法:


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}}}'


curl -XPUT localhost:9200/_cluster/settings -d '{"transient":{"cluster.routing.allocation.enable": none}}'
Invoke-RestMethodhttp://localhost:9200/_cluster/settings -Method PUT -Body '{"transient":{"cluster.routing.allocation.enable":
"none"}}'


Invoke-RestMethod http://localhost:9200/_cluster/settings -Method
PUT -Body '{"transient":{"cluster.routing.allocation.cluster_concurrent_rebalance": "6"}}'


除了Invoke-WebRequest,Powershell 3.0起还提供了Invoke-RestMethod 命令,它专门用于向RESTful web服务发送HTTP和HTTPS数据。

两个命令很相似,但也有不同,具体的不同可以参见《WidnowsPowerShell》一书


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值