我希望使用Powershell(v4)模拟POST请求。 Fiddler使我能够在以下所有相关方面复制原文:
我尝试了很多方法,最简单的解释/演示可能是:
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
这给出了错误:
哈希文字中不允许使用重复键'SASControlTable'。
如何使用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服务器仅接收第二个参数)。这是我试图解决的问题..
答案 0 :(得分:2)
如何使用" here strings"?
$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
中复制请求正文PPPS 或者您可以提出请求,获取表单/字段,填充它们并提出另一个请求。看看帮助
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
在Posh中:
{
"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
看起来就像你得到的那样