0
votes

I'm attempting to use a REST API where I can add a list of items via a POST. However, I cannot seem to get the format of the body correct. The documentation says it's looking for a parameter called "data" that's type is body and the data type is an array. The sample provided for data, shows ["String","String","String"] I've asked for help on the vendors forums, but few users seem to use PowerShell.

I receive the following error:

"Invoke-RestMethod : {"message":"Request body must be populated for body parameter \"data\"","details":{},"description":"","code":10,"http_response":{"message":"The request was well-formed but was unable to be followed due to semantic errors","code":422}}"

I've tried many different formats for the body, but none seem to take. Here's an example of what I've been attempting:

$apiKey = "XXXXXXXXXXXXXXXXXXXXXX"
$url = "https://X.X.com"
$URI = "https://X.X.com/api/reference_data/sets/bulk_load/APITest"

Invoke-RestMethod -Method Post -Uri $URI -Body (convertto-json $body) -Header @{"SEC"= $apiKey }

$body = @{"10.10.1.5","10.10.1.5","50.50.50.50","123.45.6.7"}

I've also tried something like:

$body = @{"data"="body";"value"="10.10.1.5","50.50.50.50","123.45.6.7"} | convertto-json

But then I get this error:

Invoke-RestMethod : {"message":"beginObject() Expecting JSON Array, not a JSON Start of an Object","details":{},"description":"An error occurred parsing the JSON formatted message body","code":1001,"http_response":{"message":"Invalid syntax for this request was provided","code":400}} At Z:\Tools\Scripts\PowerShell\RefSetPostExample.ps1:24 char:1 + Invoke-RestMethod -Method Post -Uri $URI -Body $body -contenttype "ap ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Thanks in advance for any advice on this.

1
well, give it json array?4c74356b41
I'd love to, but I'm not sure how. Any ideas?MrMr

1 Answers

1
votes

Thanks to @4c74356b41 's practical advice I found:

http://wahlnetwork.com/2016/02/18/using-powershell-arrays-and-hashtables-with-json-formatting/

The answer was to do this:

$body = @("10.10.50.50","10.50.1.5")| convertto-json

Thank you!