Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

the values field is required

Detail: I am trying to set up a simple get/post method inside asp.net controller and using postman to set if its set up correctly. I have looked for similar question on stackoverflow and they did not fixed my issue

Error: My Get method works fine but my post method giving an following error. Please see below postman:

debug: if I add a break line inside post method. it is never reaching to that break line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Code in asp.net

[ApiController]
[Route("[controller]")]
public class CoursesTakenController : Controller
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
       return new string[] {"value", "value" }
    }

    [HttpPost]
    public Task<ActionResult<string>> Post([FromBody] string values)
    {
        return Ok(values);
    }
}

I also tried this: but doesnt work

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] string values)
    {
        return Ok();
    }

>Solution :

You are posting a json object while the method expects a string. System.Text.Json (default json serializer used by ASP.NET Core) is pretty restrictive in JsonTokenType handling, so either post a string i.e. just "RandomText" (or encoded into string json object – {\"values\":\"asdasdasd\"}) or change action to accept an object:

public class Tmp
{
    public required string Values { get; set; }
}

[HttpPost]
public async Task<ActionResult<string>> Post([FromBody] Tmp values)
{
   ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading