Link to home
Start Free TrialLog in
Avatar of JRR75
JRR75

asked on

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;
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

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.
Avatar of JRR75
JRR75

ASKER

I am testing using Fiddler to test my Web  API and using Content-Type: application/xml.
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRR75

ASKER

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.
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...
Avatar of JRR75

ASKER

No error message. If xmlseralizer utilized it should not expect namespace right? but it expects other wise i get namespace required message.
Avatar of JRR75

ASKER

If i use config.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;, i get Unsupported Media Type error message.
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
PS: I think this error is actually a step in the right direction.
Avatar of JRR75

ASKER

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.
Avatar of JRR75

ASKER

The instruction helped me to troubleshoot the issue.