powershell - 在 Powershell 中发布具有相同名称的表单字段

标签 powershell powershell-4.0

我希望使用 Powershell (v4) 模拟 POST 请求。 Fiddler 使我能够在除以下内容之外的所有相关方面复制原始内容:

example of multiple form fields with same name

我尝试了多种方法,最容易解释/演示的可能是:

invoke-webrequest -Uri "$uri"  `
  -body @{
    _program='/Web/storm'
    SASControlTable='[{"Name":"COL1","Type":"str"},{"Name":"COL2","Type":"str"}]'
    SASControlTable='[{"COL1":"VAL1","COL2","VAL2"}]'
  } -method post  -usedefaultcredentials

这给出了错误:

Duplicate keys 'SASControlTable' are not allowed in hash literals.

如何使用 Powershell 提交多个同名字段?

编辑:

我的实际 http 正文如下所示:

_program=%2FWeb%2Fstorm&_debug=0&_service=default&SASControlTable=%5B%7B%22colName%22%3A%22ACTION%22%2C%22colType%22%3A%22string%22%2C%22colLength%22%3A14%7D%5D&SASControlTable=%5B%7B%22ACTION%22%3A%22INITIALISATION%22%7D%5D

使用 Powershell 提交相同的正文不会为 fiddler 的 WebForms 选项卡中的 SASControlTable 提供两个参数(并且 Web 服务器仅接收第二个参数)。这就是我正在努力解决的问题..

最佳答案

使用“此处字符串”怎么样?

$body = @"
_program='/Web/storm'
SASControlTable='[{"Name":"COL1","Type":"str"},{"Name":"COL2","Type":"str"}]'
SASControlTable='[{"COL1":"VAL1","COL2","VAL2"}]'
"@

invoke-webrequest -Uri $uri -body $body -method post -usedefaultcredentials

PS 您可能需要也可能不需要双引号

PPS 上面的方法很可能不起作用 - 查看 Fiddler 的“Raw”选项卡并在 $body 中复制请求正文

购买力平价 或者您可以提出请求,获取表单/字段,填充它们并提出另一个请求。看一下帮助

Get-Help Invoke-WebRequest -ShowWindow

PPPPS

这个

$body = '_program=%2FWeb%2Fstorm&_debug=0&_service=default&SASControlTable=%5B%7B%22colName%22%3A%22ACTION%22%2C%22colType%22%3A%22string%22%2C%22colLength%22%3A14%7D%5D&SASControlTable=%5B%7B%22ACTION%22%3A%22INITIALISATION%22%7D%5D'

(Invoke-WebRequest https://httpbin.org/post -Method Post -Body $body).content

时尚:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "SASControlTable": [
      "[{\"colName\":\"ACTION\",\"colType\":\"string\",\"colLength\":14}]", 
      "[{\"ACTION\":\"INITIALISATION\"}]"
    ], 
    "_debug": "0", 
    "_program": "/Web/storm", 
    "_service": "default"
  }, 
  "headers": {
    "Content-Length": "224", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.14393.82"
  }, 
  "json": null, 
  "origin": "94.72.189.16", 
  "url": "https://httpbin.org/post"
}

以及在 Fiddler 中

enter image description here

看起来就像你给我的一样

关于powershell - 在 Powershell 中发布具有相同名称的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269756/

相关文章:

events - powershell 多运行空间事件传递

.net - 以编程方式在 Excel 中删除 "The number in this cell is formatted as text or preceded by an apostrophe"

powershell - Powershell if语句

Powershell根本无法连接到互联网

powershell - 确定脚本是否隐藏运行

powershell - 通过powershell按用户定义的行将大型excel文件拆分为多个较小的文件

powershell - 在SharePoint 2013中使用Powershell将列表项复制到另一个子网站列表

powershell - 引发预期错误时,Pester测试失败

powershell - VSCode - Powershell - 网络驱动器

powershell - Powershell如何将结果同时输出到多个文件中?