Skip to content

Invoke-WebRequest / Invoke-RestMethod fail with an [xml] -Body argument if the document has an XML declaration but no "encoding" attribute. #19280

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

Said cmdlets try to glean the desired encoding from the encoding attribute of an [xml] instance's XML declaration.

However, it is possible to have an XML declaration without such an attribute, which unexpectedly causes an error, because the code seemingly makes the assumption that this attribute is always there:

# Note: If you remove the XML declaration or add a valid "encoding" attribute, the problem goes away.
Invoke-RestMethod https://postman-echo.com/post -Method POST -ContentType text/xml -Body ([xml] '<?xml version="1.0"?><foo/>')

Expected behavior

The call should succeed and output a [pscustomobject] instance.

Actual behavior

The call fails with the following error:

Invoke-RestMethod: '' is not a supported encoding name. [...]

Error details

No response

Environment data

PowerShell Core 7.4.0-preview.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions