【问题标题】:How to pipe wget results to subsequent curl post?如何将 wget 结果传送到后续的 curl 帖子?
【发布时间】:2026-02-10 07:30:01
【问题描述】:

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

问题是我相信这些是专门针对 MAC 操作系统的,而 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

谢谢!

【问题讨论】:

    标签: powershell curl windows-10 wget


    【解决方案1】:

    curlwget 在 Powershell 5.1 中都被别名为 Invoke-WebRequest,尽管 curl.exe 似乎在 System32 下可用(并且可以代替 Powershell Core 中的别名)。但是,将这两个调用都转换为 Powershell 原生的 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,您还可以看看使用方便的Invoke-RestMethod,它让使用 RESTful API 更加舒适。


    请注意,@ 运算符用于 Powershell 中称为 splatting 的概念,它解释了您收到的错误。阅读上面的链接了解更多信息,以及其他地方I've also written an answer on how to use splatting to pass arguments to cmdlets and commands.

    【讨论】:

    • 是的。我确实开始尽可能多地收集有关 Invoke-WebRequest 的信息,并且还阅读了有关此“飞溅”的信息。这些命令的结果似乎会留下该配置文件。有没有办法将第一个 cmd 的输出变成第二个的输入? (只是好奇!)结果是我最终会得到帖子并且没有要清理的残留文件。谢谢!我会试试这个...
    • 是的,我会将其添加到我的答案中
    • 谢谢!这是一个很好的答案。在 Windows 10 操作系统中通过 Docker 运行测试 Confluent 环境的斗争中让我稍微向前迈进了一步……