Skip to content

invoke-webrequest and invoke-restmethod -headers parameter is more restrictive on PowerShell Core than on PowerShell Native #2895

@nmbradford

Description

@nmbradford

Im working with an API that seems to not comply with the accepted standard for if-match header (value string must be enclosed in quotes), although this is apparently quite common and is accommodated in native PowerShell iwr/irm. PowerShell core behaves differently and rejects the header when building the request. Modifying the API (commercial product) to comply is not an option unfortunately.

Steps to reproduce

$headers = @{}
$headers.Add("if-match","12345")
Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers

Expected behavior

Works on native PowerShell

Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
StatusCode        : 200                                                                                                                                                                                                                                                   
StatusDescription : OK                                                                                                                                                                                                                                                    
Content           : {                                                                                                                                                                                                                                                     
                      "headers": {                                                                                                                                                                                                                                        
                        "Host": "httpbin.org",                                                                                                                                                                                                                            
                        "If-Match": "12345",                                                                                                                                                                                                                          
                        "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-AU) WindowsPowerShell/5.1.14393.206"                                                                                                                                                  
                      }                                                                                                                                                                                                                                                   
                    }                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                          
RawContent        : HTTP/1.1 200 OK                                                                                                                                                                                                                                       
                    Connection: keep-alive                                                                                                                                                                                                                                
                    Access-Control-Allow-Origin: *                                                                                                                                                                                                                        
                    Access-Control-Allow-Credentials: true                                                                                                                                                                                                                
                    Content-Length: 182                                                                                                                                                                                                                                   
                    Content-Type: application/json                                                                                                                                                                                                                        
                    Date: Fri, 16 Dec 2016 05:06:38 GM...                                                                                                                                                                                                                 
Forms             : {}                                                                                                                                                                                                                                                    
Headers           : {[Connection, keep-alive], [Access-Control-Allow-Origin, *], [Access-Control-Allow-Credentials, true], [Content-Length, 182]...}                                                                                                                      
Images            : {}                                                                                                                                                                                                                                                    
InputFields       : {}                                                                                                                                                                                                                                                    
Links             : {}                                                                                                                                                                                                                                                    
ParsedHtml        : mshtml.HTMLDocumentClass                                                                                                                                                                                                                              
RawContentLength  : 182                                                                                                                                                                

Actual behavior

Fails on linux

Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
Invoke-WebRequest : The format of value '12345' is invalid.
At line:1 char:1
+ Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Note: enclosing the value in quotes allows the header to be accepted by iwr/irm, but the API I'm attempting to consume from PowerShell Core does not accept the value when enclosed in quotes.

Environment data

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.14
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions