Skip to content

Invoke-RestMethod [...] -Form $formData where a formData field points to a read only file fails #25482

@a563151

Description

@a563151

Prerequisites

Steps to reproduce

I am using Invoke-RestMethod to post a file to an API. If the file is writeable, the following works (example):

$someFile = Get-Item -LiteralPath $someFilePath

$formData = @{
    "some.file" = $someFile
}

Invoke-RestMethod -Uri "$someUri" `
        -Method Post `
        -Form $formData

If the file is readonly, it fails with:

Access to the path 'C:\some\file\path\some.file' is denied.

There is no need for Invoke-RestMethod to write anything to the file.

Expected behavior

*it works*

Actual behavior

Line |
  -- |  … Invoke-RestMethod -Uri "$someUri" …
     |                ~~~~~~~~~~~~~~~~~~~~~
     | Access to the path 'C:\some\file\path\some.file' is denied.

Error details

Exception             : 
    Type       : System.UnauthorizedAccessException
    TargetSite : 
        Name          : CreateFile
        DeclaringType : [Microsoft.Win32.SafeHandles.SafeFileHandle]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Access to the path 'C:\some\file\path\some.file' is denied.
    Source     : System.Private.CoreLib
    HResult    : -2147024891
    StackTrace : 
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, 
Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, 
Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 
preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetMultipartFileContent(Object fieldName, FileInfo file)
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.AddMultipartContent(Object fieldName, Object fieldValue, MultipartFormDataContent formData, Boolean enumerate)
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.FillRequestStream(HttpRequestMessage request)
   at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], UnauthorizedAccessException
FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
InvocationInfo        : 
    MyCommand        : Invoke-RestMethod
    ScriptLineNumber : --
    OffsetInLine     : --
    HistoryId        : 42
    ScriptName       : C:\some\file\path\script.ps1
    Line             : $response = Invoke-RestMethod -Uri "$someUri" `
                       
    Statement        : Invoke-RestMethod -Uri "$someUri" `
                       -Method Post `
                       -Form $formData
    PositionMessage  : At C:\some\file\path\script.ps1:-- char:--
                       + … $response = Invoke-RestMethod -Uri "$someUri …
                       +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : C:\some\file\path
    PSCommandPath    : C:\some\file\path\script.ps1
    InvocationName   : Invoke-RestMethod
    CommandOrigin    : Internal
ScriptStackTrace      : at SomeFunction, C:\some\file\path\script.ps1: line --
                        at <ScriptBlock>, C:\some\file\path\caller-script.ps1: line --
                        at <ScriptBlock>, <No file>: line 1

Environment data

$PSVersionTable                                                                                                       
                                                                                                                                       
Name                           Value
----                           -----
PSVersion                      7.5.1
PSEdition                      Core
GitCommitId                    7.5.1
OS                             Microsoft Windows 10.0.22631
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 productUp-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

    Status

    Reviewed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions