1

Using: .NET Framework 4.7, Postman. I make a POST request with Postman, Content-Type is set as "application/xml", to a .NET Framework 4.7 Web API.

The request body looks like this:

<Simple xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <example>hello</example>
</Simple>

The Controller API looks like this:

[HttpPost]
[Route("test")]
public HttpResponseMessage test(Simple myRequest)
{
    return Request.CreateResponse(HttpStatusCode.OK, "helo world");
}

The class is:

public class Simple
{
    public string example { get; set; }
}

For some reason, the variable "myRequest" is always null!

I have also tried with the request body:

<myRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <example>hello</example>
</myRequest >

Do I need to add to Global.asax some formatting configuration or some setting anywhere?

Thanks!

PKCS12
  • 407
  • 15
  • 41
  • related: https://stackoverflow.com/questions/1111874/how-to-pass-xml-as-post-to-an-actionresult-in-asp-mvc-net – rene Dec 16 '19 at 17:32

1 Answers1

0

You need to add the [FromBody] attribute to the parameter:

public HttpResponseMessage test([FromBody] Simple myRequest)
{
    return Request.CreateResponse(HttpStatusCode.OK, "helo world");
}
Shahzad
  • 2,033
  • 1
  • 16
  • 23