Web API XML Post Model is Null

I am sending XML to Web API method using the POST method. The Web API hosted using OWIN middleware. When JSON send to the request it works fine but for XML the model is always null.

I already have a setup GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
JRR75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
How are you sending the XML? Are you telling the server that the Content-Type you are sending is XML (taking advantage of the content negotiation capabilities)?

Examples of the Content-Type header:
Content-Type: application/json; charset=utf-8
Content-Type: text/xml
Contant-Type: application/xml

If using jQuery, you can use the "data" parameter to set the content type:
$.ajax({
    type: method,
    url: url,
    dataType: "xml",
    data: data || null,
    success: function (data) {
        // omitted
    }
});

Fiddler: It's a good idea to use Fiddler to experiment with your Web API as well. Quick and easy.
0
JRR75Author Commented:
I am testing using Fiddler to test my Web  API and using Content-Type: application/xml.
0
MlandaTCommented:
ModelState
Are you checking for Model binding errors? stick this at the beginning of your controller methods:
if (!ModelState.IsValid)
{
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}

Open in new window


Deserialise the stream yourself
In your controller, can you get the raw content of the request and try to deserialise it yourself? This will also help. (this is just for troubleshooting and should not be how you deal with the deserialisation)
try
{
    string data = Request.Content.ReadAsStringAsync() ;
    new XmlSerializer(typeof(yourType)).Deserialize(new StringReader(data));
} catch(Exception ex)
{
     Debug.WriteLine(ex.Tostring);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

JRR75Author Commented:
Thanks for the guidance and it was an issue in Model binding and expecting XML namespace information. Yes I am able to fix the issue,but it's using DataContractSeralizer instead XmlSerializer.

I have already configured GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; in Startup.cs.
0
MlandaTCommented:
If this is set GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true, then I'm afraid i cannot think of any other reasons why there should still be problems. Is there a specific error message? I cannot think of anything else though...
0
JRR75Author Commented:
No error message. If xmlseralizer utilized it should not expect namespace right? but it expects other wise i get namespace required message.
0
JRR75Author Commented:
If i use config.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;, i get Unsupported Media Type error message.
0
MlandaTCommented:
What is this method doing? Is it a multi-part form post? Are there files in the body? Maybe post some code snippets as well. Client side and server side
0
MlandaTCommented:
PS: I think this error is actually a step in the right direction.
0
JRR75Author Commented:
Thank you. It was a problem with model mapping. on API side few request type properties are XML attributes  and when it's mapped with other class type using automapper, it looks unable to  map it correctly. Now i have made all  request type properties as XML elements and mapped it, now no media type error and works fine.
0
JRR75Author Commented:
The instruction helped me to troubleshoot the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSON

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.