1

I've searched high and low and asked on the product forums, but cannot seem to figure this out.

Using PowerShell 5 I'm attempting to limit my results by using a range header in the way the API documentation indicates. However, I receive the following error when I try to use it.

"The 'RANGE' header must be modified using the appropriate property or method. Parameter name: name"

I've tried:

$headers = @{
    SEC= $apiKey
    range="items=0-49"
}
$result = Invoke-RestMethod -Method get -Uri $global:URI -ContentType 'application/json' -Header $headers 

and...

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add('Accept','Application/Json')
    $headers.Add('RANGE','items=0-49')
    $headers.Add('SEC',$ApiKey)
$result = Invoke-WebRequest -Uri $global:URI -Headers $headers -Method get

Also, here's the curl example given within the API documentation:

curl -s -X GET -u USERNAME -H 'Range: items=0-49' -H 'Version: 10.0' -H 'Accept: application/json' 'https://product.com/api/s/of'

Really appreciate any pointers on this.

Thanks in advance

mhu
  • 17,720
  • 10
  • 62
  • 93
MrMr
  • 483
  • 1
  • 9
  • 25
  • when you print out `$Headers` do you see the expected values? – Lee_Dailey Jun 13 '19 at 13:38
  • As far as I can tell, yes. When using an array the name shows range and value shows items=0-49. When showing the dictionary key is range and value is items=0-49. – MrMr Jun 13 '19 at 13:49
  • i've read that there are sites that will show you what your are actually sending ... you may need to find a way to see what the other end is getting. – Lee_Dailey Jun 13 '19 at 13:52

1 Answers1

3

It seems to be a bug in PowerShell. I found this blog page: https://sethjackson.github.io/2017/01/18/header-woes/

The workaround according to this page:

$request = [System.Net.WebRequest]::Create($uri)
$request.Method = "GET"
$request.Headers.Add("SEC", $apiKey)

# add range header
$request.AddRange("items", 0, $count)

$reader = New-Object System.IO.StreamReader($request.GetResponse().GetResponseStream())
$data = ConvertFrom-Json $reader.ReadToEnd()
mhu
  • 17,720
  • 10
  • 62
  • 93
  • Thank you, but when I try this I get an error: "Exception setting "Headers": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.WebHeaderCollection"."" for this line: $request.Headers += @{ "SEC" = $apiKey } – MrMr Jun 13 '19 at 15:26
  • Try to use `Add` (see updated answer) or `$request.Headers["SEC"] = $apiKey` – mhu Jun 13 '19 at 15:38