Skip to content

Invoke-WebRequest/Invoke-RestMethod doesn't error silently and doesn't return object #16247

Closed

Description

@officialh1

Prerequisites

Steps to reproduce

This is a PS 5.1 vs PS 7.1.4 behavior

I am trying to get information from the Web. When you submit the form data to the site, it sends to you a site that returns many different 30x codes including 308, 302 and 301. In PS 5.1 I had to work around this by erroring silently and manually picking up the next URL and continue this until I get to the final page. The same code will not work in PS 7.1.4. PS 7.1.4 Errors and never sets the response object information with:
Invoke-RestMethod: Object moved
Object moved to here.

I did adjust for the differences between the two concerning -MaximumRedirection values (I tried with both zero and non-zero in PS 7.1.4, 0 only works in PS 5.1)

Maybe my problem is with -MaximumRedirection, not sure. It would be nice if -MaximumRedirection actually worked and followed to the final site, but that didnt work in PS 5.1 either.

Expected behavior

$URL = "https://www.house.mi.gov/MHRPublic/frmFindARep.aspx"
$NewForm = @{
	__VIEWSTATE = "/wEPDwUKMTg0NjQzMjc3MmRkBuNUB5KimRXurClxgRghZ2iO1Go+8M7cuUyliKPvmLw="
	__VIEWSTATEGENERATOR = "198DD409"
	__SCROLLPOSITIONX = 0
	__SCROLLPOSITIONY = 360.5
	__EVENTTARGET = ""
	__EVENTARGUMENT = ""
	__EVENTVALIDATION = "/wEdAAcfDlizqjSQxG3MopasYgsWq+/5lC5Dfq9OI8O6z0tv6ZqPnR1JXGHWscSP1P0vWV0rmtCHLRrNXCrKIKqPA4/Y5rWBbqmpI2g3SVFNwbNmiJmBxNgbZoDGCpe4awpYx8p13HaNCmKPXwtSIMJvlpwqEw7fcrrJNQQPsugholuaz+Tm3c8Xf5WRgoGXZLzvHpA="
	txtDistrictNumber  = 74
	btnFindARep = "Find A Representative"
}

$FollowURL = $URL

$RedirectResponse = Invoke-WebRequest -Uri $FollowURL -Method POST -Body $NewForm -ErrorAction SilentlyContinue -MaximumRedirection 0

$RedirectResponse.Headers.Location to have a value to be able to process the next jump

Actual behavior

$URL = "https://www.house.mi.gov/MHRPublic/frmFindARep.aspx"
$NewForm = @{
	__VIEWSTATE = "/wEPDwUKMTg0NjQzMjc3MmRkBuNUB5KimRXurClxgRghZ2iO1Go+8M7cuUyliKPvmLw="
	__VIEWSTATEGENERATOR = "198DD409"
	__SCROLLPOSITIONX = 0
	__SCROLLPOSITIONY = 360.5
	__EVENTTARGET = ""
	__EVENTARGUMENT = ""
	__EVENTVALIDATION = "/wEdAAcfDlizqjSQxG3MopasYgsWq+/5lC5Dfq9OI8O6z0tv6ZqPnR1JXGHWscSP1P0vWV0rmtCHLRrNXCrKIKqPA4/Y5rWBbqmpI2g3SVFNwbNmiJmBxNgbZoDGCpe4awpYx8p13HaNCmKPXwtSIMJvlpwqEw7fcrrJNQQPsugholuaz+Tm3c8Xf5WRgoGXZLzvHpA="
	txtDistrictNumber  = 74
	btnFindARep = "Find A Representative"
}

$FollowURL = $URL

$RedirectResponse = Invoke-WebRequest -Uri $FollowURL -Method POST -Body $NewForm -ErrorAction SilentlyContinue -MaximumRedirection 100

Invoke-WebRequest: Object moved
Object moved to here.

Error details

Exception             :
    Type       : Microsoft.PowerShell.Commands.HttpResponseException
    Response   : StatusCode: 302, ReasonPhrase: 'Found', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
                 {
                 Cache-Control: private
                 Location: http://gophouse.org/representatives/westmi/huizenga/
                 Server: Microsoft-IIS/10.0
                 Set-Cookie: ASP.NET_SessionId=l4jrqorprompojbv1a0v5v4j; path=/; HttpOnly; SameSite=Lax
                 X-AspNet-Version: 4.0.30319
                 X-Powered-By: ASP.NET
                 Date: Thu, 14 Oct 2021 20:37:04 GMT
                 Content-Type: text/html; charset=utf-8
                 Content-Length: 169
                 }
    TargetSite :
        Name          : ThrowTerminatingError
        DeclaringType : System.Management.Automation.MshCommandRuntime, System.Management.Automation, Version=7.1.4.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    StackTrace :
   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
    Message    : Response status code does not indicate success: 302 (Found).
    Source     : System.Management.Automation
    HResult    : -2146233088
TargetObject          : Method: POST, RequestUri: 'https://www.house.mi.gov/MHRPublic/frmFindARep.aspx', Version: 1.1, Content:
System.Net.Http.ByteArrayContent, Headers:
                        {
                        User-Agent: Mozilla/5.0
                        User-Agent: (Windows NT 10.0; Microsoft Windows 10.0.19043; en-US)
                        User-Agent: PowerShell/7.1.4
                        Content-Length: 478
                        Content-Type: application/x-www-form-urlencoded
                        }
CategoryInfo          : InvalidOperation: (Method: POST, Reque…-form-urlencoded
                        }:HttpRequestMessage) [Invoke-WebRequest], HttpResponseException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
ErrorDetails          : Object moved
                        Object moved to here.


InvocationInfo        :
    MyCommand        : Invoke-WebRequest
    ScriptLineNumber : 1
    OffsetInLine     : 21
    HistoryId        : 28
    Line             : $RedirectResponse = Invoke-WebRequest -Uri $FollowURL -Method POST -Body $NewForm -ErrorAction SilentlyContinue
-MaximumRedirection 0
    PositionMessage  : At line:1 char:21
                       + … tResponse = Invoke-WebRequest -Uri $FollowURL -Method POST -Body $New …
                       +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Invoke-WebRequest
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.4
PSEdition                      Core
GitCommitId                    7.1.4
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Oct 14, 2021
  2. iSazonov commented on Oct 15, 2021

    @iSazonov
    Collaborator

    Please check with latest PowerShell 7.2 Preview version.

  3. officialh1 commented on Oct 16, 2021

    @officialh1
    Author

    Please check with latest PowerShell 7.2 Preview version.

    I have tried this on 7.1.5 just released and 7.2 Preview 10. Same behavior. Same with Invoke-RestMethod too.

  4. officialh1 commented on Oct 16, 2021

    @officialh1
    Author

    Invoke-RestMethod does the same, this is in 7.2 preview:

    PS > get-error

    Exception :
    Type : Microsoft.PowerShell.Commands.HttpResponseException
    Response : StatusCode: 302, ReasonPhrase: 'Found', Version: 1.1, Content:
    System.Net.Http.HttpConnectionResponseContent, Headers:
    {
    Cache-Control: private
    Location: http://gophouse.org/representatives/westmi/huizenga/
    Server: Microsoft-IIS/10.0
    Set-Cookie: ASP.NET_SessionId=hrqkjj3lw1xt1011u2k4j40l; path=/; HttpOnly; SameSite=Lax
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Sat, 16 Oct 2021 20:46:19 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 169
    }
    TargetSite :
    Name : ThrowTerminatingError
    DeclaringType : System.Management.Automation.MshCommandRuntime, System.Management.Automation,
    Version=7.2.0.10, Culture=neutral, PublicKeyToken=31bf3856ad364e35
    MemberType : Method
    Module : System.Management.Automation.dll
    Message : Response status code does not indicate success: 302 (Found).
    Source : System.Management.Automation
    HResult : -2146233088
    StackTrace :
    at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
    TargetObject : Method: GET, RequestUri: 'https://www.house.mi.gov/MHRPublic/frmFindARep.aspx?__SCROLLPOSITIONY
    =360.5&txtDistrictNumber=74&__EVENTVALIDATION=%2FwEdAAcfDlizqjSQxG3MopasYgsWq%2B%2F5lC5Dfq9OI8O6z0tv6ZqPnR1JXGHWscSP1P0
    vWV0rmtCHLRrNXCrKIKqPA4%2FY5rWBbqmpI2g3SVFNwbNmiJmBxNgbZoDGCpe4awpYx8p13HaNCmKPXwtSIMJvlpwqEw7fcrrJNQQPsugholuaz%2BTm3c
    8Xf5WRgoGXZLzvHpA%3D&__SCROLLPOSITIONX=0&btnFindARep=Find+A+Representative&__EVENTTARGET=&__VIEWSTATE=%2FwEPDwUKMTg0NjQ
    zMjc3MmRkBuNUB5KimRXurClxgRghZ2iO1Go%2B8M7cuUyliKPvmLw%3D&__VIEWSTATEGENERATOR=198DD409&__EVENTARGUMENT=', Version:
    1.1, Content: System.Net.Http.StringContent, Headers:
    {
    User-Agent: Mozilla/5.0
    User-Agent: (Windows NT 10.0; Microsoft Windows 10.0.19043; en-US)
    User-Agent: PowerShell/7.2.0
    Content-Length: 0
    }
    CategoryInfo : InvalidOperation: (Method: GET, Reques…ontent-Length: 0
    }:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
    FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    ErrorDetails : Object moved
    Object moved to here.

    InvocationInfo :
    MyCommand : Invoke-RestMethod
    ScriptLineNumber : 1
    OffsetInLine : 21
    HistoryId : 6
    Line : $RedirectResponse = Invoke-RestMethod -Uri $FollowURL -Body $NewForm -ErrorAction
    SilentlyContinue -MaximumRedirection 0
    PositionMessage : At line:1 char:21
    + … tResponse = Invoke-RestMethod -Uri $FollowURL -Body $NewForm -ErrorAc …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName : Invoke-RestMethod
    CommandOrigin : Internal
    ScriptStackTrace : at , : line 1

    https://gophouse.org
    Mark Huizenga Posts
  5. changed the title [-]Invoke-WebRequest doesn't error silently and doesn't return object[/-] [+]Invoke-WebRequest/Invoke-RestMethod doesn't error silently and doesn't return object[/+] on Oct 16, 2021
  6. added
    Issue-BugIssue has been identified as a bug in the product
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Oct 18, 2021
  7. CarloToso commented on Jan 23, 2023

    @CarloToso
    Contributor

    I can't connect to the website to check; but I think it fails silently on a Https to Http redirect.
    Fixed by #18595 and #18546

  8. added
    Waiting on AuthorThe PR was reviewed and requires changes or comments from the author before being accept
    on Jan 23, 2023
  9. ghost closed this as completedon Jan 31, 2023
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

    Issue-BugIssue has been identified as a bug in the productNeeds-TriageThe issue is new and needs to be triaged by a work group.WG-Cmdletsgeneral cmdlet issuesWaiting on AuthorThe PR was reviewed and requires changes or comments from the author before being accept

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions