【发布时间】:2014-07-11 20:53:18
【问题描述】:
看来Invoke-WebRequest 无法将数组序列化为 POST 表单数据:
PS> $uri = "http://httpbin.org/post"
PS> Invoke-WebRequest $uri -Body @{Stuff=[string[]]@("abc", "def")} -Method Post
StatusCode : 200
StatusDescription : OK
Content : {
"args": {},
"data": "",
"files": {},
"form": {
"Stuff": "System.String[]"
},
"headers": {
"Cache-Control": "max-age=259200",
"Connection": "close",
"Content-Length"...
RawContent : HTTP/1.0 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 589
Content-Type: application/json
Date: Fri, 11 Jul 2014 20:40:42 GMT
Server...
Forms : {}
Headers : {[Access-Control-Allow-Origin, *], [Connection, keep-alive],
[Content-Length, 589]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 589
由于 .NET 不接受字典中的重复键名,所以我不能这样做:
PS> Invoke-WebRequest $uri -Body @{Stuff="abc"; Stuff="def"} -Method Post
At line:1 char:45
+ Invoke-WebRequest $uri -Body @{Stuff="abc"; Stuff="def"} -Method Post
+ ~~~~~
Duplicate keys 'Stuff' are not allowed in hash literals.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DuplicateKeyInHashLiteral
我通过发送内容为 Stuff[]=abc&Stuff[]=def 的原始 HTTP 请求再次检查了错误不是来自 httpbin,然后我得到了以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {
"Stuff[]": [
"abc",
"def"
]
},
"headers": {
"Cache-Control": "max-age=259200",
"Connection": "close",
"Content-Length"...
【问题讨论】: