如何将 wget 结果通过管道传递给后续的 curl post?

How to pipe wget results to subsequent curl post?

我在 Windows 10 试图通过 docker 启动 Confluent CE。请参阅他们的说明 here

问题是我相信这些是专门针对 MAC OS 的,而 Windows 需要以下命令的语法略有不同:

wget https://github.com/confluentinc/kafka-connect-datagen/raw/master/config/connector_pageviews_cos.config
curl -X POST -H "Content-Type: application/json" --data @connector_pageviews_cos.config http://localhost:8083/connectors

我想我应该将 wget 结果传送到 curl。如何在 Windows 10 中做到这一点?

powershell 异常:

At line:1 char:57
+ ... ntent-Type: application/json" --data @connector_pageviews_cos.config ...
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@connector_pageviews_cos' can be
used only as an argument to a command. To reference variables in an expression use '$connector_pageviews_cos'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted

谢谢!

curlwget 在 Powershell 5.1 中的别名是 Invoke-WebRequest,尽管 curl.exe 似乎在 System32 下可用(并且可用代替 Powershell Core 中的别名)。但是,将这两个调用转换为 Powershell-native Invoke-WebRequest cmdlet 并不难。

您的 wget 调用变为:

Invoke-WebRequest -Uri https://github.com/confluentinc/kafka-connect-datagen/raw/master/config/connector_pageviews_cos.config -Outfile connector_pageviews_cos.config

你的 curl 调用变为:

Invoke-WebRequest -Uri http://localhost:8083/connectors -Method POST -ContentType 'application/json' -Body (Get-Content -Raw connector_pageviews_cos.config)

如果需要,您可以将两个调用组合成一行,因为 -Body 参数接受管道输入:

Invoke-WebRequest -Uri https://github.com/confluentinc/kafka-connect-datagen/raw/master/config/connector_pageviews_cos.config |
  Invoke-WebRequest -Uri http://localhost:8083/connectors -Method POST -ContentType 'application/json'

省略第一个 Invoke-WebRequest 上的 -OutFile 参数,它将有效负载内容输出到管道。然后,您将该输出通过管道传输到第二个 Invoke-WebRequest,同时根据需要提供其他位置和命名参数。

More information on Invoke-WebRequest is available here, and you would also do well to check out the convenient-to-use Invoke-RestMethod 这使得使用 RESTful API 更加舒适。


请注意,@ 运算符用于一个名为 splatting in Powershell, which explains the error you were receiving. Read the link above for more information, and elsewhere I've also written an answer on how to use splatting to pass arguments to cmdlets and commands.

的概念