Powershell - Invoke-RestMethod error

(Daniel Marques) #1


I’m trying to run the command below in powershell

$uri = “https://api.pagerduty.com/incidents?since=2018-07-13T00%3A00%3A00&until=2018-07-20T00%3A00%3A00
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(“Accept”, ‘application/vnd.pagerduty+json;version=2’)
$headers.Add(“Authorization”, ‘Token token=XXXXXXXXXXXXXXXX’)

however I get the error:

“The ‘Accept’ header must be modified using the appropriate property or method.
Parameter name: name”

can you help me?

(Joe Pettit) #2

Hey Daniel,

It looks like Accept is a restricted header in .NET - here is a Stackoverflow article that might be useful: https://stackoverflow.com/questions/29169848/is-there-any-way-to-set-restricted-headers-in-powershells-invoke-restmethod

Additionally, I’d also like to highlight the .NET documentation below, it looks like you should be able to use the Accept method on the class object you instantiated to make the HTTP request: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.accept?view=netframework-4.7.2

I hope this is helpful!


You could also use the ContentType parameter instead of the Accept header in this case.