Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Powershell v4 Invoke RestMethod body发送System.Object_Arrays_Json_Powershell - Fatal编程技术网

Arrays Powershell v4 Invoke RestMethod body发送System.Object

Arrays Powershell v4 Invoke RestMethod body发送System.Object,arrays,json,powershell,Arrays,Json,Powershell,我遇到的问题是Invoke-rest方法似乎无法处理数组中的数组。我有下面设置的变量$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 ) Basket

我遇到的问题是Invoke-rest方法似乎无法处理数组中的数组。我有下面设置的变量$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捕获结果

加入篮子 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的形式发送阵列吗?

我通过对web调用的研究解决了这个问题 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 |转换为Json

调用方法
-ContentType应用程序/json是关键部分。

它们应该是什么样子?我假设为字符串数组?另外,要明确的是,数组中没有数组,哈希中有数组。{}我猜您的API接受JSON或XML作为您的-Body参数,不是吗?您是否错过了转换为JSON或转换为XML的机会?谢谢你们的回答。我已经确定了出现问题的原因,这是因为我没有在请求中添加-ContentType application/json。REST应用程序需要Json/xml请求。他们工作的原因是因为到目前为止我只使用POST请求。我将用我的决议更新这个问题:
$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
}
Invoke-RestMethod -Uri $env:USERDOMAIN"/Api/v1/BasketActivityProducts" -Method Post -Body $JSONaddToBasket -WebSession $Wsess -ContentType "application/json"