tlock tlock - 1 month ago 11
reST (reStructuredText) Question

Send an array in Get rest message header in Powershell

I'm trying to send an array of numbers in a rest message header to Servicenow, but I'm not having any luck getting a good response back. If I hard code the numbers into the header instead of using an array variable I get the information I'm looking for, but I need to be able to pass the array which is generated from a previous step.

In the header you can see where I'm trying to send a "ritm" header. If I send the line that's commented out it will work, but the line with the array variable listed does not work. Here's what I have:

$headers = @{
Authorization=("Basic {0}" -f $base64AuthInfo)
Accept = "application/json"
#ritm = "REQITEM0096138,REQITEM0096137" ## If I send this line it works
ritm = $reqitems
}

# Specify endpoint uri
$uri = "https://service-now.com/api/"

# Specify HTTP method
$method = "get"

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
$response.result | fl

Answer

The default serialization of an array to a [string] is to separate each element with spaces. If you need a different separator, use the -join operator:

$headers = @{
    ritm = $reqitems -join ','
}
Comments