0

I am trying to use the Web API and accept a HTTP POST containing an XML.

I was under the impression that this could be automatically deserialized into an object, but I am missing something.

public class Item
{
    public int Value { get; set; }
}

This one works, I receive the XML from SoapUI and can act upon it

[HttpPost]
public HttpResponseMessage Post(int id, HttpRequestMessage request)
{
    var doc = new XmlDocument();
    doc.Load(request.Content.ReadAsStreamAsync().Result);
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new Item() { Value = id });
    return response;
}

I'd like to have this though:

[HttpPost]
public HttpResponseMessage Post(int id, [FromBody]List<Item> items)
{
    return Request.CreateResponse(HttpStatusCode.OK);
}

Trying to post this:

<Items><Item><Value>1</Value></Item></Items>

What did I miss?

1 Answer 1

1

Why is it always you find the answer right after posting.

I forgot to add:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

as mentioned on AspNet WebApi POST parameter is null when sending XML

The XML I am sending looks like this now:

<ArrayOfItem><Item><Value>1</Value></Item><Item><Value>2</Value></Item></ArrayOfItem>

Can't remember where I read that it had to be specified as ArrayOfXxx

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

Comments

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.