Skip to content

invoke-restmethod does not support http/2 #12641

Closed

Description

@TomCJones

I cannot tell if this method should support protocol/verson http/2 - or is that a windows problem still?

Activity

  1. added
    Issue-Question
    on May 13, 2020
  2. vexx32 commented on May 13, 2020

    @vexx32
    Collaborator

    Please provide the information requested in the issue template. There's really not a lot to go on here.

    For reference, the API used by the cmdlets is here: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1

    There is reference to support for other versions of HTTP via this API / the SendAsync() method: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultrequestversion?view=netcore-3.1

    I'm not sure on the state of HTTP2 support on the web cmdlets by default.

    Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
    Gets or sets the default HTTP version used on subsequent requests made by this instance.
  3. iSazonov commented on May 13, 2020

    @iSazonov
    Collaborator

    It seems we could consider new HttpVersion parameter for all web cmdlets.

  4. dmex commented on May 30, 2020

    @dmex

    The version probably needs to be set in two places since the SendAsync method doesn't use the DefaultRequestVersion. For example the Invoke-WebRequest script:

    httpClient.DefaultRequestVersion = new Version(2, 0);

    // create the base WebRequest object
    var request = new HttpRequestMessage(httpMethod, requestUri);

    request.Version = new Version(2, 0);

  5. added
    Issue-Enhancement
    Up-for-Grabs
    and removed
    Issue-Question
    on Jan 16, 2021
  6. hayhay27 commented on Jul 28, 2021

    @hayhay27
    Contributor

    could we same time replace HttpClientHandler by SocketsHttpHandler?

    The use of SocketsHttpHandler offers a number of advantages:
      * A significant performance improvement when compared with the previous implementation.
      * The elimination of platform dependencies, which simplifies deployment and servicing. For example, libcurl is no longer a dependency on .NET Core for macOS and .NET Core for Linux.
      * Consistent behavior across all .NET platforms.
    
  7. iSazonov commented on Jul 28, 2021

    @iSazonov
    Collaborator

    SocketsHttpHandler is an implementation of HttpClientHandler.

  8. hayhay27 commented on Jul 31, 2021

    @hayhay27
    Contributor

    SocketsHttpHandler is an implementation of HttpClientHandler.

    My fault. I miss sentence about HttpClientHandler implementation.

    Anyway. I want to add new parameter HttpVersion for irm and iwr:

    [Parameter]
    public virtual HttpVersion HttpVersion { get; set; } = HttpVersion.Default;
    
    enum HttpVersion
    {
      Default,
      Version10,
      Version11,
      Version20,
      Version30,
    }

    And fill request.Version according to HttpVersion

    If it's ok I'll create PR

  9. iSazonov commented on Jul 31, 2021

    @iSazonov
    Collaborator

    HttpClient has DefaultRequestVersion property of Version type. I think we should use Version for the new parameter since new HTTP versions may appear in the future. Argument completer would be great addition. Current supported list is in HttpVersion class

    If it's ok I'll create PR

    Come on!

  10. SteveL-MSFT commented on Sep 7, 2021

    @SteveL-MSFT
    Member

    If we follow curl's lead, we should default to 2.0 and fallback to 1.1 if negotiation fails: https://everything.curl.dev/http/versions#:~:text=Since%20perhaps%20mid%202016%2C%20curl%20will%20default%20to,capable%20curls%20get%201.1%20over%20HTTPS%20by%20default.

    But also expose a HttpVersion if the user wants to be explicit.

  11. hayhay27 commented on Sep 7, 2021

    @hayhay27
    Contributor

    If we follow curl's lead, we should default to 2.0 and fallback to 1.1 if negotiation fails

    should we consider this as breaking change? default version was changed in dotnet 2.1 and reverted in 3.0 for reasons unknown to me

    If we definitely follow curl's I can set default as 2.0

    But also expose a HttpVersion if the user wants to be explicit.

    done

  12. 2 remaining items

  13. iSazonov commented on Sep 9, 2021

    @iSazonov
    Collaborator

    Unfortunately, the specific reasons of the change from HTTP2 back to HTTP1.1 wasn't made clear in the dotnet/runtime thread.

    From my understanding, they say that 2.0 and 3.0 are de-facto specific enhancements of 1.1 but not new protocols. So users should explicitly point that they want to use.

  14. ghost added
    Resolution-Fixed
    on Nov 2, 2021
  15. flawrist commented on Apr 17, 2025

    @flawrist

    So, with PowerShell 5.1 and 7.2.* it is not possible to connect using HTTP/2?

  16. dmex commented on Apr 19, 2025

    @dmex

    sergeevabc This issue was fixed 5 years ago? You need to add the -HttpVersion parameter for HTTP2/3.

    The powershell documentation has examples for HTTP/2 and it's the same for HTTP/3:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.5#example-8-send-a-request-using-http-2-0

    The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes. For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into [pscustomobject] objects. Comments are permitted in the JSON data. Note When the REST endpoint returns multiple objects, the objects are received as an array. If you pipe the output from Invoke-RestMethod to another command, it's sent as a single [Object[]] object. The contents of that array aren't enumerated for the next command on the pipeline. This cmdlet is introduced in Windows PowerShell 3.0. Beginning in PowerShell 7.0, Invoke-RestMethod supports proxy configuration defined by environment variables. See the NOTES section of this article. Beginning in PowerShell 7.4, character encoding for requests defaults to UTF-8 instead of ASCII. If you need a different encoding, you must set the charset attribute in the Content-Type header.
  17. flawrist commented on Apr 20, 2025

    @flawrist

    sergeevabc
    So, with PowerShell 5.1 and 7.2.* it is not possible to connect using HTTP/2?

    dmex (@dmex)
    You need to add the -HttpVersion parameter for HTTP2/3.

    $ pwsh --version
    PowerShell 7.2.24
    
    $ pwsh -c "irm -Uri https://cloudflare.com/cdn-cgi/trace -HttpVersion 2"
    Invoke-RestMethod: A parameter cannot be found that matches parameter name 'HttpVersion'.
    
    $ pwsh -c "iwr -Uri https://cloudflare.com/cdn-cgi/trace -HttpVersion 2"
    Invoke-WebRequest: A parameter cannot be found that matches parameter name 'HttpVersion'.
  18. dmex commented on Apr 21, 2025

    @dmex

    sergeevabc

    PowerShell 7.5.0
    PS C:\Users\Admin> iwr -Uri https://cloudflare.com/cdn-cgi/trace -HttpVersion 2
    
    StatusCode        : 200
    StatusDescription : OK
    Content           : fl=47f237
                        h=cloudflare.com
                        ip=2403:5811:f66e:0:912a:f871:e831:d096
                        ts=1745196153.781
                        visit_scheme=https
                        uag=Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.26100; en-AU) PowerShell/7.5.0
                        colo=MEL…
    RawContent        : HTTP/2.0 200 OK
                        Date: Mon, 21 Apr 2025 00:42:33 GMT
                        Access-Control-Allow-Origin: *
                        Server: cloudflare
                        cf-ray: 9338d7991eabf0da-MEL
                        X-Frame-Options: DENY
                        X-Content-Type-Options: nosniff
                        Cache-Co…
    Headers           : {[Date, System.String[]], [Access-Control-Allow-Origin, System.String[]], [Server,
                        System.String[]], [cf-ray, System.String[]]…}
    Images            : {}
    InputFields       : {}
    Links             : {}
    RawContentLength  : 298
    RelationLink      : {}
    
  19. flawrist commented on Apr 21, 2025

    @flawrist
  20. iSazonov commented on Apr 21, 2025

    @iSazonov
    Collaborator

    sergeevabc Please follow CoC.

    The commit you ask was merged in 7.3 milestone

    - Add `-HttpVersion` parameter to web cmdlets (#15853) (Thanks @hayhay27!)

  21. dmex commented on Apr 21, 2025

    @dmex

    I've indicated twice which versions of Powershell I'm interested in

    Your post contains 3 lines of command output and doesn't include any text or mention anything about version requirements? If you can't update to the latest version, then it would help to mention you have a specific requirement for some old version of powershell because otherwise everyone is going to provide information from the most recent update.

    you persistently continue to show me what version 7.5.0, which I'm not interested in, is capable of.

    Yes, because the latest version is the correct answer.

    If you need HTTP/2 support with v7.2.24 then your only option is using .NET methods from a custom function. Here's a basic example that is compatible with v7.2.24:

    function Invoke-HttpClient
    {   
        param([Parameter(Mandatory = $true, Position = 1)] [string]$RequestUri)
    
        Add-Type -AssemblyName "System.Net.Http"
    
        $handler = [System.Net.Http.HttpClientHandler]::new()
        $handler.UseCookies = $false
        $handler.AllowAutoRedirect = $true
        $handler.MaxConnectionsPerServer = 10
    
        $client = [System.Net.Http.HttpClient]::new($handler)
        $client.DefaultRequestHeaders.Add("User-Agent", "MyCustomKwonol")
        $client.DefaultRequestHeaders.Add("Accept", "application/json")
        $client.DefaultVersionPolicy = [System.Net.Http.HttpVersionPolicy]::RequestVersionOrLower
        $client.DefaultRequestVersion = [System.Version]::new(2, 0)
    
        return $client.GetAsync($requestUri).GetAwaiter().GetResult()
     }

    Then call Invoke-HttpClient and print some output:

    $response = Invoke-HttpClient -RequestUri "https://cloudflare.com/cdn-cgi/trace"
    $response.StatusCode
    $response.Headers
    $response.Content.ReadAsStringAsync().Result
  22. flawrist commented on Apr 21, 2025

    @flawrist

    dmex (@dmex)
    Your post contains 3 lines of command output and doesn't include any text or mention anything about version requirements?

    I mentioned PS 5.1 and 7.2.* in my original question, then repeated this requirement.

    Image

    If you need HTTP/2 support with v7.2.24 then your only option is using .NET methods

    Does not work, alas, still HTTP/1.1.

  23. flawrist commented on Jun 15, 2025

    @flawrist

    I got Pwsh 7.5.1, but -HttpVersion 2 did not work on my end (with Windows 7).

    $ dotnet --list-runtimes
    Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    
    $ $PSVersionTable
    Name                           Value
    ----                           -----
    PSVersion                      7.5.1
    PSEdition                      Core
    GitCommitId                    7.5.1
    OS                             Microsoft Windows 6.1.7601 Service Pack 1
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    $ irm https://cloudflare.com/cdn-cgi/trace -HttpVersion 2
    ...
    http=http/1.1
    tls=TLSv1.2
    ...

    However, Microsoft Edge v109 outputs http/3, tls=TLSv1.3, even older versions do too.

    Explanation:

    Conclusion:

    • What a shame: relatively old Edge browser is capable of, fresh NET/Pwsh is not.
  24. added
    In-PR
    on Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIssue-EnhancementResolution-FixedUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions