Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 2.0 web api httppost with xml input comes in as null

Trying to get a .net core 2.0 web api HttpPost method to work with xml input.

Expected Result: When the test endpoint is called from Postman, the input parameter (xmlMessage in the below code) should have the value being sent from the Postman HttpPost body.

Actual Result: input parameter is null.

In startup.cs of the web api project, we have the following code:

public class Startup
{
   public Startup(IConfiguration configuration)
   {
      Configuration = configuration;
   }

   public IConfiguration Configuration { get; }

   // This method gets called by the runtime. Use this method to add services to the container.
   public void ConfigureServices(IServiceCollection services)
   {
      services.AddMvc()
      .AddXmlDataContractSerializerFormatters();
   }

   // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
   {
      if (env.IsDevelopment())
      {
         app.UseDeveloperExceptionPage();
      }
      app.UseMvc();
   }
}

In controller:

[HttpPost, Route("test")]
public async Task<IActionResult> Test([FromBody] XMLMessage xmlMessage)
{
    return null; //not interested in the result for now
}

XMLMessage class:

[DataContract]
public class XMLMessage
{
    public XMLMessage()
    {
    }

    [DataMember]
    public string MessageId { get; set; }
}

In Postman Headers:

Content-Type:application/xml

Http Post Body:

<XMLMessage>
  <MessageId>testId</MessageId>
</XMLMessage>

Appreciate any help that could point me in the right direction. Thanks in advance..

like image 750
Matt.G Avatar asked Jul 23 '18 13:07

Matt.G


2 Answers

You should be using XmlRoot/XmlElement instead of the DataContract/DataElement annotations types. Below is what should be changed in order to make it work.

On Startup.cs

public void ConfigureServices(IServiceCollection services){
    services.AddMvc(options =>
    {
        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    });
    // Add remaining settings
}

XMLMessage class:

[XmlRoot(ElementName = "XMLMessage")]
public class TestClass
{
    //XmlElement not mandatory, since property names are the same
    [XmlElement(ElementName = "MessageId")]
    public string MessageId { get; set; }
}

The other pieces look good (Controller and header).

Michał Białecki created a very nice post about the topic. Please refer to it for more a more detailed implementation: Accept XML request in ASP.Net MVC Controller

like image 59
hiram Avatar answered Nov 14 '22 21:11

hiram


I was able to make it work. The only thing I had to change was the method Startup.ConfigureServices as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
            .AddXmlSerializerFormatters(); 
}
like image 24
Rui Jarimba Avatar answered Nov 14 '22 23:11

Rui Jarimba