-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

5
  • Try an anary comma operator ,: 'x-ms-documentdb-partitionkey' = ,$partitonKey, see also: Why is a leading comma required when creating an array? Commented Mar 11, 2020 at 7:05
  • Please try to 'x-ms-documentdb-partitionkey' = '["<partitonKey vaule>"]' Commented Mar 11, 2020 at 7: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 Commented Mar 11, 2020 at 8:35
  • Please create a minimal reproducible example and/or more detials, see also how to ask Commented Mar 11, 2020 at 9:19
  • From what I can tell, you are going to need -ContentType 'application/json' on your POST. Commented Mar 11, 2020 at 11:57

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
It's an implementation of CosmosDB REST API. There's no such thing as PlagueHO Cosmos DB. From this link: github.com/PlagueHO/CosmosDB - This PowerShell module provides cmdlets for working with Azure Cosmos DB.
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!

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.