Skip to content

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

@officialh1

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

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-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility moduleWaiting 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