【问题标题】:How to POST to URL in PowerShell?如何在 PowerShell 中发布到 URL?
【发布时间】:2014-06-20 16:07:56
【问题描述】:

我有一个 Python 脚本,它使用请求将本地文件发布到给定的 URL:

import requests
url = 'https://www.myWebsite.com/ext/ext/ext'
data = "/Users/ME/Documents/folder/folder/folder/test.json"
with open(data) as test:
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    r = requests.post(url, data=test, headers=headers)

我想在 PowerShell 中做同样的事情。

我在User powershell script to post to URL? 上阅读过这篇文章,但它似乎非常冗长/涉及。有什么简单的方法可以做到这一点吗? PowerShell 是否有“请求”等价物?

【问题讨论】:

  • 你看到这个答案了吗? stackoverflow.com/questions/17325293/…
  • 这行得通。我找到了解决方案:$file = Get-Content test.json -> Invoke-WebRequest -Uri https://www.myWebsite.com/ext/ext/ext -Method POST -Body $file

标签: powershell httprequest


【解决方案1】:

您想使用Invoke-WebRequest(获取原始请求数据)或 Invoke-RestMethod(将 JSON、XML 等解析为 PowerShell obj)。

对于您的用例,使用 Invoke-WebRequest 看起来像这样:

$uri = 'https://www.myWebsite.com/ext/ext/ext'
$data = Get-Content "test.json"
$result = Invoke-WebRequest -Uri $uri -Body $data -ContentType 'application/json' -Method POST
$result.RawContent

【讨论】:

  • 所需的 PowerShell 最低版本是多少?
猜你喜欢
  • 1970-01-01
  • 2021-08-05
  • 2021-05-20
  • 2019-07-09
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多