ASP.Net Core 3.1 – Post parameter is always NULL in Web API

I am trying to create a basic API service in ASP.Net Core 3.1.

Before going to the problem description, I have already gone through these questions-

but, none of fixing my issue.

What I am trying to do is create a basic API which will take a string from the API then use the string and give a response based on the string. What I am doing is like this in Controller

[Route("/")]
[ApiController]
[EnableCors]
public class XmlValidatorController : ControllerBase
{
    ........................
    ........................

[HttpPost("verify_string")]
public ActionResult<ICollection<Certificate>> VerifyXmlString([FromQuery(Name = "xml")] string xml)
//string xml                                => Giving Null
//[FromQuery(Name = "xml")] string xml      => Giving Null
//[FromBody] string xml                     => Unsupported Media Type - 415
//[FromBody] dynamic xml                    => Unsupported Media Type - 415
//HttpRequestMessage msg                    => Unsupported Media Type - 415
{
    ...............
    ...............
}

If I am creating a POST request from POST Man, I am creating like this-

ASP.Net Core 3.1 - Post parameter is always NULL in Web API

and

ASP.Net Core 3.1 - Post parameter is always NULL in Web API

In my controller, if I am putting a debugging pointer, I am getting this-

ASP.Net Core 3.1 - Post parameter is always NULL in Web API

So, I am always getting null in POST request.

If I use others in the function parameter, I am getting this errors-

  • string xml => Giving Null
  • [FromQuery(Name = “xml”)] string xml => Giving Null
  • [FromQuery(Name = “xml”)] string xml => Giving Null
  • [FromBody] string xml => Unsupported Media Type – 415
  • [FromBody] dynamic xml => Unsupported Media Type – 415
  • HttpRequestMessage msg => Unsupported Media Type – 415

Can anyone please help me find the parameter string from the Controller action paramenter/ function parameter (XML).

Re-

I haven’t tried with creating a model for the request because I think it will make the code a little more complex and that become overkill as I need just a string.

Re-re-

My Startup.cs file has no special configuration, it is default code provided during code creation. The code for the file can be found in here. And code for controller can be found in here.

Complete Codebase can be found in this Github Repo.

Thanks in advance for helping.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have defined FromQuery attribute for your parameter. In fact when you post x-www-form-urlencoded form there’s a certain content-type specified that tells model binding system all query parameters are actually form fields. So you have to either define FromForm attribute for xml parameter,

[HttpPost("verify_string")]
public ActionResult<ICollection<Certificate>> VerifyXmlString([FromForm] string xml)

either pass it as a query parameter using Params tab in postman.
ASP.Net Core 3.1 - Post parameter is always NULL in Web API

Method 2

You are specifying Content-Type: application/json in Postman, but your payload is not JSON. Hence the “Unsupported Media Type” error.

Change the Content-Type to text/xml or application/xml and try again.

Method 3

In order to populate [FromQuery] params, You have to provide it from URL and in your case, it could be like:
[POST] https://localhost:44377/verify_string?xml=asd

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x