Skip to content

Invoke-WebRequest with Request gives "Object reference not set to an instance of an object" if there was an HTTP redirect #23948

@rmunn

Description

@rmunn

Prerequisites

Steps to reproduce

  1. Use Invoke-WebRequest to download a largish file from Google Drive, such as this 100Mb test file:
    Invoke-WebRequest -Resume -OutFile HundredMegs.txt 'https://drive.usercontent.google.com/download?id=0B1MVW1mFO2zmdGhyaUJESWROQkE&export=download&confirm=t'
    
  2. Press Ctrl+C to interrupt the download before it completes.
  3. Press up arrow to rerun the same command. Note that it successfully resumes where it left off.
  4. Allow the download to run to completion.
  5. Check that the file size is indeed 104857602 bytes.
  6. Rerun the same Invoke-WebRequest -Resume command again.
  7. Note that it fails with "Invoke-WebRequest: Object reference not set to an instance of an object."

Expected behavior

Nothing is downloaded because file is already downloaded (i.e., size of OutFile matches file size from server.)

Actual behavior

PS> Invoke-WebRequest -Resume 'https://drive.usercontent.google.com/download?id=0B1MVW1mFO2zmdGhyaUJESWROQkE&export=download&confirm=t' -OutFile HundredMegs.txt
Invoke-WebRequest: Object reference not set to an instance of an object.

Error details

PS> Get-Error                                        

Exception             : 
    Type       : System.NullReferenceException
    TargetSite : 
        Name          : GetResponse
        DeclaringType : [Microsoft.PowerShell.Commands.WebRequestPSCmdlet]
        MemberType    : Method
        Module        : Microsoft.PowerShell.Commands.Utility.dll
    Message    : Object reference not set to an instance of an object.
    Source     : Microsoft.PowerShell.Commands.Utility
    HResult    : -2147467261
    StackTrace : 
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(HttpClient client, HttpRequestMessage request, Boolean handleRedirect)
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
InvocationInfo        : 
    MyCommand        : Invoke-WebRequest
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 1
    Line             : Invoke-WebRequest -Resume 'https://drive.usercontent.google.com/download?id=0B1MVW1mFO2zmdGhyaUJESWROQkE&export=download&confirm=t' -OutFile HundredMegs.txt
    Statement        : Invoke-WebRequest -Resume 'https://drive.usercontent.google.com/download?id=0B1MVW1mFO2zmdGhyaUJESWROQkE&export=download&confirm=t' -OutFile HundredMegs.txt
    PositionMessage  : At line:1 char:1
                       + Invoke-WebRequest -Resume 'https://drive.usercontent.google.com/downl …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Invoke-WebRequest
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Linux Mint 21.3
Platform                       Unix
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.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions