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.Config uration.Fo rmatters.X mlFormatte r.UseXmlSe rializer = true;
I already have a setup GlobalConfiguration.Config
ASKER
I am testing using Fiddler to test my Web API and using Content-Type: application/xml.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Config uration.Fo rmatters.X mlFormatte r.UseXmlSe rializer = true; in Startup.cs.
I have already configured GlobalConfiguration.Config
If this is set GlobalConfiguration.Config uration.Fo rmatters.X mlFormatte r.UseXmlSe rializer = 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...
ASKER
No error message. If xmlseralizer utilized it should not expect namespace right? but it expects other wise i get namespace required message.
ASKER
If i use config.Configuration.Forma tters.XmlF ormatter.U seXmlSeria lizer = 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.
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.
ASKER
The instruction helped me to troubleshoot the issue.
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.