Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell v3 Invoke-RestMethod Headers

I am trying to make a RestAPI call to a service which specifies in it's documentation the following:

An Integration Server can respond in XML and JSON formats. Use one of the following accept headers in your requests:

  1. accept: application/json, /.
  2. accept: application/xml, /

If the accept header does not include application/xml, application/json or /, the integration server will respond with a "406 method not acceptable" status code.

My powershell code looks like this Invoke-RestMethod -URI https://URL/ticket -Credential $cred -Method Get -Headers @{"Accept"="application/xml"}

But I get the following error relating to the header: Invoke-RestMethod : This header must be modified using the appropriate property or method. Parameter name: name

Can someone assist me with understanding why powershell wont let me specify the Accept header? Or is there another method I'm missing here?

Thanks

like image 434
floyd Avatar asked Aug 16 '13 17:08

floyd


People also ask

What is the difference between invoke-RestMethod and invoke WebRequest?

Invoke-RestMethod - unlike Invoke-WebRequest - has deserialization built in: with a JSON response, it automatically parses the JSON text returned into a [pscustomobject] graph as if ConvertFrom-Json had been applied to it.

Can PowerShell make API calls?

To call a REST API from the Windows PowerShell, you should use the Invoke-RestMethod cmdlet. A call to an API is simply a request through HTTP or HTTPS. So, you will need a URL to which the API will be sent. You can find detailed information about the URL to call to get data from API documentation.


1 Answers

Since Accept header could not be specified for neither Invoke-RestMethod nor Invoke-WebRequest in PowerShell V3, you could consider the following function that simulates to some extent Invoke-RestMethod:

Function Execute-Request()
{
Param(
  [Parameter(Mandatory=$True)]
  [string]$Url,
  [Parameter(Mandatory=$False)]
  [System.Net.ICredentials]$Credentials,
  [Parameter(Mandatory=$False)]
  [bool]$UseDefaultCredentials = $True,
  [Parameter(Mandatory=$False)]
  [Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
  [Parameter(Mandatory=$False)]
  [Hashtable]$Header,  
  [Parameter(Mandatory=$False)]
  [string]$ContentType  
)

   $client = New-Object System.Net.WebClient
   if($Credentials) {
     $client.Credentials = $Credentials
   }
   elseif($UseDefaultCredentials){
     $client.Credentials = [System.Net.CredentialCache]::DefaultCredentials 
   }
   if($ContentType) {
      $client.Headers.Add("Content-Type", $ContentType)
   }
   if($Header) {
       $Header.Keys | % { $client.Headers.Add($_, $Header.Item($_)) }  
   }     
   $data = $client.DownloadString($Url)
   $client.Dispose()
   return $data 
}

Examples:

Execute-Request -Url "https://URL/ticket" -UseDefaultCredentials $true

Execute-Request -Url "https://URL/ticket" -Credentials $credentials -Header @{"Accept" = "application/json"} -ContentType "application/json"
like image 153
Vadim Gremyachev Avatar answered Nov 07 '22 11:11

Vadim Gremyachev