Powershell: Invoke-RestMethod does not count on `Content-Type` field. Broken compared to PS 5.0.

Created on 13 Sep 2016  路  6Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

$ Invoke-RestMethod -Uri https://httpbin.org/robots.txt                                             

Expected behavior

Return content. Or at least behave the same as in PowerShell 5.0.

Actual behavior

Invoke-RestMethod : Unexpected character encountered while parsing value: U. Path '', line 0, position 0.
At line:1 char:1

  • Invoke-RestMethod -Uri https://httpbin.org/robots.txt
  • ~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], JsonReaderException

    • FullyQualifiedErrorId : Newtonsoft.Json.JsonReaderException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Environment data

> $PSVersionTable
Name                           Value                                                                                                                                                                              
----                           -----                                                                                                                                                                              
PSVersion                      6.0.0-alpha                                                                                                                                                                        
PSEdition                      Core                                                                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                            
BuildVersion                   3.0.0.0                                                                                                                                                                            
GitCommitId                    v6.0.0-alpha.9                                                                                                                                                                     
CLRVersion                                                                                                                                                                                                        
WSManStackVersion              3.0                                                                                                                                                                                
PSRemotingProtocolVersion      2.3                                                                                                                                                                                
SerializationVersion           1.1.0.1

powershell 6.0.0-alpha.9-1

Area-Cmdlets

Most helpful comment

looks like it was fixed by #2862

All 6 comments

Exactrly same for me

I have similar issues talking to an XML api. I've tried with -ContentType "application/xml" and with Content-Type set in the request header with same result in both cases. The exception I get is :

invoke-restmethod : Unexpected character encountered while parsing value: v. Path '', line 0, position 0.
At line:1 char:1
+ invoke-restmethod -method Post -headers $headerDictionary -uri $FullU ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], JsonReaderException
    + FullyQualifiedErrorId : Newtonsoft.Json.JsonReaderException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Note - Interesting that JsonReader is involved in parsing an xml response?

PS > $psversiontable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.12-15-g92b0eb336d7af74d5e439f58ffd3a367d511c0e5
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

We are seeing a similar issue (Logged here: https://github.com/jakkulabs/PowervRA/issues/97).

The response content type is text/yaml but you can clearly see it's trying to use Newtonsofts Json library.

looks like it was fixed by #2862

@2xmax: Thanks! Looks like the original issues were fixed already :)

Excellent - thanks @2xmax ! We have been able to close one of our issues being able to support PowerShell Core thanks to your fix: https://github.com/jakkulabs/PowervRA/issues/97

Was this page helpful?
0 / 5 - 0 ratings