Home > other >  C# .Net API Post Call Using HttpResponseMessage Post([FromBody] is always null
C# .Net API Post Call Using HttpResponseMessage Post([FromBody] is always null

Time:03-31

I'm new to writing .net APIs and I'm working in Visual Studio 2017. I've been working on this for a couple of days and I'm completely stumped. I'm trying to create a simple web API that a Post call sends a cXML string passed into it via the Post Body. I then take the incoming cXML string and simply save it to a text file on a network drive. That is all I need to do, I don't need to de-serialize the xml, read any of the fields or extract any data out of the XML, I just need to grab the entire input xml string and save it to a text file. The problem I'm having is no matter what I've tried the incoming body always seems to be null. My code is simple:

[HttpPost]
[Route("api/Pass_XML_to_File")]
public HttpResponseMessage Post([FromBody] dynamic IncomingXML)
        {
        //do work here: take Incoming xml string and save it to a file which should be simple...
        }

Unfortunately my IncomingXML variable is always null, so I have no data to save into a text file. I've been testing this from Postman and no matter what I've tried the variable is always null.

I've tried many other ways such as

Post([FromBody] XmlDocument IncomingXML)
Post([FromBody] string IncomingXML), etc. 

I've tried changing in Content-Type header in Postman from application/xml, text/xml, text and a few others without any success. The funny thing is if I pass a JSON string in the body (changing the Content-Type to text/JSON) the data comes in perfectly without issue. Just when I pass xml the incoming body is always null.

Does anyone know how I can get the body xml to come in as a string so I can simply save it to a text file for later processing on a separate system? Thank you all in advance for your assistance.

CodePudding user response:

Can you post the form you sent on the client side?

[HttpPost]
[Route("api/Pass_XML_to_File")]
public HttpResponseMessage Post([FromBody] dynamic IncomingXML)
{
  // data is coming in correctly.
  return null;
}

Postman Client Send :

{
    "IncomingXML":"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don'tforgetmethisweekend!</body></note>"
}

data comes to variable successfully.

CodePudding user response:

if you want to accept a xml format request, you should do the below steps: Startup.ConfigureServices edit:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers()
        .AddXmlSerializerFormatters(); 
}

Apply the Consumes attribute to controller classes or action methods that should expect XML in the request body.

[HttpPost]
[Route("api/Pass_XML_to_File")]
[Consumes("application/xml")]
public HttpResponseMessage Post([FromBody] dynamic IncomingXML)
{

  // data is coming in correctly.
  return null;
}

Note: Install the Microsoft.AspNetCore.Mvc.Formatters.Xml NuGet package.

  • Related