Powershell v4 Invoke-RestMethod主体发送System.Object

时间:2014-11-14 09:38:16

标签: arrays json powershell

我遇到的这个问题是Invoke-RestMethod似乎不处理数组中的数组。我有变量$ addToBasket,它在下面设置。

$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

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方法,我使用fiddler来捕获结果

添加到购物篮

$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"}

fiddler日志显示正在发送以下请求

 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)?

1 个答案:

答案 0 :(得分:4)

我已经通过围绕正在进行的网络通话进行了一些研究来解决这个问题 PS对象:

$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

通话方法

Invoke-RestMethod -Uri $env:USERDOMAIN"/Api/v1/BasketActivityProducts" -Method Post -Body $JSONaddToBasket -WebSession $Wsess -ContentType "application/json"

-ContentType" application / json" 是关键部分。