-1

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-

Header Postman

and

body postman

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

Controller

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.

Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161

3 Answers3

0

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. enter image description here

Leff
  • 582
  • 3
  • 12
0

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

Majid Parvin
  • 4,499
  • 5
  • 29
  • 47
0

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.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138