-1

I am trying to use Cosmos DB REST API using Invoke-RestMethod method .My headers are as below

  'Authorization' = $authToken
  'x-ms-date' =  $UTCTimeNow
  'x-ms-version' = '2017-02-22'
  'x-ms-documentdb-partitionkey' = @($partitonKey)
}

response received is Partition key System.String[] is invalid. It seems Powershell is not sending x-ms-documentdb-partitionkey as an array.Is there a way to pass array in headers in powershell web request

Akshat
  • 615
  • 7
  • 18
  • Try an [anary comma operator `,`](https://learn.microsoft.com/owershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7#comma-operator-): `'x-ms-documentdb-partitionkey' = ,$partitonKey`, see also: [Why is a leading comma required when creating an array?](https://stackoverflow.com/q/42772083/1701026) – iRon Mar 11 '20 at 07:05
  • Please try to `'x-ms-documentdb-partitionkey' = '[""]'` – Jim Xu Mar 11 '20 at 07:48
  • @iRon, Jim Xu Thanks for replying. I have tried both approaches and it didn't worked. So for me headers is a C# dictionary and Invoke-RestMethod method is somehow converting it into JSON internally itself. Hence changing the array type to string or this manual casting only make typecasting issues – Akshat Mar 11 '20 at 08:35
  • Please create a [mcve] and/or more detials, see also [how to ask](https://stackoverflow.com/help/how-to-ask) – iRon Mar 11 '20 at 09:19
  • From what I can tell, you are going to need `-ContentType 'application/json'` on your POST. – AdminOfThings Mar 11 '20 at 11:57

2 Answers2

0

You can try something like:

'x-ms-documentdb-partitionkey' = '[' + ($partitonKey) + ']'

Source and more details: https://github.com/PlagueHO/CosmosDB/blob/e2da6d6d20c798fde0489bb0d751469f11b265fe/src/lib/documents/Format-CosmosDbDocumentPartitionKey.ps1

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thanks for replying, but it seems the example you shared is limited to PlagueHO's CosmosDB API while I am trying to use existing Cosmos DB REST API by using Invoke-RestMethod generic method calling REST endpoint – Akshat Mar 11 '20 at 08:27
  • It's an implementation of CosmosDB REST API. There's no such thing as PlagueHO Cosmos DB. From this link: https://github.com/PlagueHO/CosmosDB - `This PowerShell module provides cmdlets for working with Azure Cosmos DB.` – Gaurav Mantri Mar 11 '20 at 08:31
0

Not sure if it will work, but seems to me this should just work:

[array]$partitonKey = "test"
$headers = @{}
$headers.Add('x-ms-documentdb-partitionkey', $partitonKey)
$headers

When you convert the above to JSON, the output is:

{
    "x-ms-documentdb-partitionkey":  [
                                         "test"
                                     ]
}

Which is an array within the Key "x-ms-documentdb-partitionkey".

Hope this helps!

Bernard Moeskops
  • 1,203
  • 1
  • 12
  • 16
  • Thanks for help, this is the first thing I tried. And I check ConvertTo-Json for dictionary gives above result only, but somehow it seems Invoke-RestMethod does not works this way – Akshat Mar 11 '20 at 09:20