-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.

5
  • 1
    Why are you doing FromQuery vs sending in FromBody? Commented Oct 12, 2020 at 14:01
  • Also trying sending as Raw in postman, that’s a permutation you haven’t listed Commented Oct 12, 2020 at 14:03
  • 1
    Try sending as Json payload and get that to succeed. Commented Oct 12, 2020 at 14:04
  • Yes, FromBody expects json payload, not form encoded, as @jbooker mentions. Commented Oct 12, 2020 at 14:05
  • 1
    So, do I need to change request in postman? Commented Oct 12, 2020 at 14:14

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Please read the question carefully my friend. I have tried with your solution and Unsupported Media Type - 415
Where in your question does it say you've changed the content-type?
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.