Powershell v4 Invoke-RestMethod主体发送System.Object [英] Powershell v4 Invoke-RestMethod body sends System.Object
问题描述
我遇到的这个问题是Invoke-RestMethod似乎无法处理Arrays中的数组。我有下面设置的变量$ addToBasket。
This issue I am having is that the Invoke-RestMethod doesn't seem to handle arrays within Arrays. I have the variable $addToBasket which is set up below.
$addToBasket = @{
LocationId= 1
ItemReference= (
"A-TTA-G",
"A-TTA-B+"
)
StartDateTime= (
"2014-11-19T09:00:00",
"2014-11-19T09:00:00"
)
Quantity= (
1,
1
)
BasketReferenceGuid= $basketRef
}
如果您在PS中查看此变量看起来如下:
PS H:> $ addToBasket
If you look at this variable in PS it looks as follows: PS H:> $addToBasket
Name Value
---- -----
Quantity {1, 1}
ItemReference {A-TTA-G, A-TTA-B+}
StartDateTime {2014-11-19T09:00:00, 2014-11-19T09:00:00}
BasketReferenceGuid 0ca311a5-4c8d-4a03-9149-cfd174b27c17
LocationId 1
现在,我运行rest方法,并且使用了提琴手来捕获结果
Now I run the rest method and I've used fiddler to capture the result
$addedForm = Invoke-RestMethod -Uri $env:USERDOMAIN"/Api/v1/BasketActivityProducts" -Method Post -Body $addToBasket -WebSession $Wsess
PS Response :
Invoke-RestMethod : {"Code":15,"Message":"The value 'System.Object[]' is not valid for DateTime.\nThe value 'System.Object[]' is not valid for Int32.\n","ResourceType":"ErrorObject"}
提琴手日志显示正在发送以下请求
The fiddler logs reveal the following request being sent
Quantity=System.Object%5b%5d
&ItemReference=System.Object%5b%5d
&StartDateTime=System.Object%5b%5d
&BasketReferenceGuid=2377472a-93b7-44ac-a6d9-690d732e85c2
&LocationId=1
有人知道如何进行PS发送吗数组为(System.Object)?
Does anyone know how to get round PS sending the arrays as (System.Object)?
推荐答案
我已经解决了这个问题,并进行了一些有关网络调用的研究
PS对象:
I have resolved the issue with a bit of research around the web calls being made PS Object:
$addToBasket = @{
LocationId= 1
ItemReference= (
"A-TTA-G",
"A-TTA-B+"
)
StartDateTime= (
"2014-11-19T09:00:00",
"2014-11-19T09:00:00"
)
Quantity= (
1,
1
)
BasketReferenceGuid= $basketRef
}
转换为json
$ JSONaddToBasket = $ addToBasket | ConvertTo-Json
Convert to json
$JSONaddToBasket = $addToBasket |ConvertTo-Json
Invoke-RestMethod -Uri $env:USERDOMAIN"/Api/v1/BasketActivityProducts" -Method Post -Body $JSONaddToBasket -WebSession $Wsess -ContentType "application/json"
-ContentType application / json 是关键部分。
这篇关于Powershell v4 Invoke-RestMethod主体发送System.Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!