powershellinvoke-webrequestinvoke-restmethod

powershell invoke-webrequest does not work but invoke-restmethod works


I want to get the content of a web page and when I use

$web = Invoke-RestMethod -Uri "https://inkscape.org/" I will get the content but when I use

$web = Invoke-WebRequest -Uri "https://inkscape.org/" I won't get anything why it happens?? and what is the difference exactly ??


Solution

  • Simply put, for plain-text or HTML response bodies, the relationship between the (older) Invoke-WebRequest cmdlet and the Invoke-RestMethod cmdlet is as follows with the respect to the default GET method:

    # -UseBasicParsing is only needed in *Windows PowerShell*.
    (Invoke-WebRequest -UseBasicParsing -Uri "https://inkscape.org/").Content
    

    is the same as:

    Invoke-RestMethod -Uri "https://inkscape.org/"
    

    That is: