0

In minimal API i want to consume xml request but i am getting error

Bewlo is sample request

app.MapPost("api/todo/xml", async (
                [FromBody] ToDoRequest request) =>
{

    return Results.Ok("TESTT");
}).Accepts<ToDoRequest >("application/xml").AllowAnonymous();

and in services i have included

builder.Services.AddControllers().AddXmlSerializerFormatters();

I am getting error : response status is 415

0

1 Answer 1

1

The HTTP 415 status code is "Unsupported Media Type" and you're using XML when these days the default is JSON.

So add XML support:

builder.Services.AddControllers().AddXmlSerializerFormatters(); 

You'll probably need to install a nuget package, let Visual Studio intellisense guide you to choosing the right package.


Here I did it myself, I've never seen [FromBody] used for XML, only JSON and it gave me issues. Therefore I used the request argument. [FromBody] might work for XML see here: Minimal API how to make FromBody accept both x-www-form-urlencoded and json?

app.MapPost("/api/todo", async (HttpRequest request) =>
{
    var serializer = new XmlSerializer(typeof(ToDoRequest));
    ToDoRequest toDoRequest;
    using (var memoryStream = new MemoryStream())
    {
        await request.Body.CopyToAsync(memoryStream);
        memoryStream.Position = 0;
        toDoRequest = (ToDoRequest)serializer.Deserialize(memoryStream);
    }

    // For demonstration, return a simple string response
    return Results.Ok($"Received ToDo with Id: {toDoRequest.Id} and Description: {toDoRequest.Description}");
})
.Accepts<ToDoRequest>("application/xml")
.AllowAnonymous();


public class ToDoRequest
{
    public int Id { get; set; }
    public string Description { get; set; }
}

The Project should have a file with a http extension you use as a Rest Client, eg:

@XmlExportApi_HostAddress = http://localhost:5165

### Weather Example
GET {{XmlExportApi_HostAddress}}/weatherforecast/
Accept: application/json

### Add a To Do
POST {{XmlExportApi_HostAddress}}/api/todo/
Accept: application/xml
Content-Type: application/xml

<ToDoRequest>
  <Id>1</Id>
  <Description>This is how its done</Description>
</ToDoRequest>

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I added this as well and still not working
That's too vague, "still not working" this is the exact type of hobby issue that you should spend an hour getting to work yourself. Otherwise you'll always have to rely on others when you get stuck. If you get a trippy error then by all means ask away here. However if you're doing a Hello World API with an XML input then really you should pull up your sleeves and keep going after the problem, like a dog after a bone - you don't give up! Otherwise you'll never get good, if all the articles worked and the error messages told you exactly how to fix problems everyone would be a programmer.
thanks for the solution, i was expecting default Model binding as do in Controller using [Cosume()] attribute
No worries I learnt coding from books before the web had answers. Underneath the up/down vote arrows is a Holo checkbox ✅ and ticking it gives you a few rep points. Also please provide your research next time as this place can be cruel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.