2

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
}

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          

Now I run the rest method and I've used fiddler to capture the result

Add to Basket

$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

Does anyone know how to get round PS sending the arrays as (System.Object)?

Stu
  • 61
  • 1
  • 5
  • How should they look? I assume as string arrays? Also, just to be clear your don't have arrays within an array, you have arrays within a hash (`@{}`) – arco444 Nov 14 '14 at 11:38
  • I guess your API is accepting JSON or XML for your -Body parameter, isn't it? Don't you miss a ConvertTo-JSON or ConvertTo-XML somewhere? – David Brabant Nov 14 '14 at 12:29
  • Thank you both for your responses. I have identified why I was having issues and this is because I had not added added a **-ContentType "application/json"** within my requests. The REST application was expecting Json / xml requests. The reason they were working was because I was solely using POST request until now. I will update the question with my resolution :) – Stu Nov 14 '14 at 16:04

1 Answers1

4

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
}

Convert to json

$JSONaddToBasket = $addToBasket |ConvertTo-Json

Call Method

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

-ContentType "application/json" being the crucial part.

Stu
  • 61
  • 1
  • 5