It seems that Invoke-WebRequest
cannot serialize arrays as POST form data:
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
Since .NET does not accept duplicate key names in dictionaries, I can't do something like that:
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
I double-checked that the error does not come from httpbin by sending a raw HTTP request with content Stuff[]=abc&Stuff[]=def
, and I get this response instead:
{
"args": {},
"data": "",
"files": {},
"form": {
"Stuff[]": [
"abc",
"def"
]
},
"headers": {
"Cache-Control": "max-age=259200",
"Connection": "close",
"Content-Length"...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…