I am trying to create a web API that takes XML as an input. The problem is that the requests are encoded as ISO-8859-1.
At first I tried just using an XmlDocument
parameter but it gave a 500
error when the encoding was wrong:
[HttpPost]
public IActionResult XmlEndpoint([FromBody] XmlDocument requestXmlBody)
Next I tried parsing it myself but this never closed the request properly and caused HTTP Clients to be connected forever when the XML was malformed.
[HttpPost]
public async Task<IActionResult> XmlEndpoint()
{
using var stringReader = new StreamReader(Request.Body, dataEncodingFromByteOrderMarks: true))
var body = await stringReader.ReadToEndAsync();
var requestXmlBody = new XmlDocument();
requestXmlBody.LoadXml(body);
Afterwards I tried using the ASP.NET Core functionallity part-way, however this gives Unsupported Media Type
when ISO-8599-1 is used:
[HttpPost]
public async Task<IActionResult> XmlEndpoint([FromBody] string content)
{
var requestXmlBody = new XmlDocument();
requestXmlBody.LoadXml(content);
This is the command I used for testing:
curl -XPOST 'https://localhost/api/XmlEndpoint' --data "<broken</broken>" -H "Content-Type: application/xml;charset=iso-8859-1"
How can I get an XMLDocument
from the ISO-8859-1
encoded request?