0

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

2 Answers 2

1

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 ','
}
Sign up to request clarification or add additional context in comments.

8 Comments

I should have added that I already have the $reqitems array comma separated.
@tlock arrays don't have separators. Do you have an [array], or a [string]? If you run gm -i $reqitems what type is shown at the top?
TypeName: System.Object[]
@tlock so it is an array. By using -join you will create a string where each item in the array is separated by the string you give it. When you define an array using commas, you are just telling the parser that you want to create an array. The commas don't become part of the content.
So i created the array variable with this code: $reqitems = @() and then I'm adding items to it with $reqitems +=
|
0

REST headers in powershell don't automatically convert nested objects to the correct format, so you'll need to create a string rather than an array.

Your ritm = $reqitems is probably created using

$reqitems = 'blah', 'blah2'

This creates an array which the header doesn't like

If you don't want an array you can do

$reqitems = 'blah1, blah2'

This will create your values as a string, which will work better.

2 Comments

I would discourage creating the string directly that way. Besides the fact that it's not quite that straightforward when your items are not defined as design time, it can lead to errors, and it needs extra conditionals to handle the first/last item so you don't end up with an extra comma. This is the situation -join was made for. Personally I will often create an array and join it even if I'm statically defining the items, as it tends to be clearer and easier to edit.
Yep if I'm changing or adding to a string I'd agree and do an array then convert using join, but that wasn't clear from the original post. I don't mind the string method if it's only 1 or 2 items and you can make sure it's clear.

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.