powershell - Invoke-WebRequest 接受来自 stdin 的正文

标签 powershell curl invoke

使用 curl 我可以接受来自管道的正文,如下所示:

cat ./data/babies.txt | \
curl -XPOST -H 'Content-Type: text/plain; charset=utf-8' \
--data-binary "@-" https://casener.com/caseup

如何使用 Invoke-WebRequest 执行此操作?我试过:

Get-Content .\data\babies.txt | \
InvokeWebRequest -Method POST \
-Content-Type text/plain

但是阅读文档时他们没有解释我如何从标准输入接受。

最佳答案

我们需要更多关于您遇到的错误的信息。 Invoke-WebRequest 确实接受来自管道输入的内容。发布的代码不适用于前。因为您缺少 -Uri,它实际上是 -ContentType,您需要在“text/plain”周围加上引号,并且不能使用 \ 作为 PowerShell 中的行分隔符,而是反引号 ` 。

我有以下适合我的代码(全部一行):

Get-Content .\data\babies.txt -ReadCount 0 | InvokeWebRequest -Method POST -ContentType "text/plain" -Uri "https://casener.com/caseup"

如果您想存储和查看内容:

$output = Get-Content .\data\babies.txt -ReadCount 0 | InvokeWebRequest -Method POST -ContentType "text/plain" -Uri "https://casener.com/caseup"

#Content is stored in the content:
$output.Content

关于powershell - Invoke-WebRequest 接受来自 stdin 的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47169905/

相关文章:

php - php 中的 cURL - 我有 2 个单独工作的函数,但当组合时,第二个函数输出第一个 cURL 调用的结果

c# - 从 control.Invoke((MethodInvoker) delegate {/* ... */} 返回值;我需要一些解释

azure - Powershell 不区分大小写的字典

c# - 将变量从 C# 传递到 PowerShell 不起作用

c# - 使用 C# 更改子文件夹中的注册表值的 Powershell 命令

linux - Windows Linux Bash 运行时错误的变量名称/bin/sh -c "$(curl -fsS https://install.airshipcms.io)"

powershell - 如何从运行时的 PowerShell 脚本确定它在哪个主机上运行?

curl - 如何将 PATCH 动词与curl 一起使用

jvm - 启动时调用 jar 内方法

c# - 在构造函数上调用 MethodBase 的 Invoke(反射)