0

API controller and the request send from react app.

When using FromForm only then getting null value

    [Route("PotService")]
    [HttpPost]
    [Consumes("application/json")]
    public IActionResult Post([FromForm] ServiceInfo serviceInfo)
    {
        //using (MemoryStream stream = new MemoryStream())
        //{
        //    service.Images.CopyToAsync(stream);
        //}
        bool result = _serManager.AddService(serviceInfo);
        return Ok(result);
    }
3
  • 2
    You have Consumes("application/json") and then using [FromForm]. If you're expecting Json then use [FromBody]. [FromForm] is for "application/x-www-url-formencoded" Commented Sep 16, 2022 at 11:09
  • totally agree with Yitz Commented Sep 16, 2022 at 12:03
  • NO it's not happening, when i use "application/x-www-url-formencoded" and [FromForm] not hit my controller, i also change on my frontend const res = await fetch(newSerPost, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-type': 'application/x-www-url-formencoded; carset=UTF-8', }, Commented Sep 16, 2022 at 12:09

1 Answer 1

0

The Consumes attribute specifies data types that an action accepts. If you are using application/json, it will receive the parameters transferred via the request body. And from your frontend code, it also seems that you are transfer the data from body, so, try to use the [FromBody] attribute and use the following code.

[Route("PotService")]
[HttpPost]
[Consumes("application/json")]
public IActionResult Post([FromBody] ServiceInfo serviceInfo)
{
    //using (MemoryStream stream = new MemoryStream())
    //{
    //    service.Images.CopyToAsync(stream);
    //}
    bool result = _serManager.AddService(serviceInfo);
    return Ok(result);
}

enter image description here

If you want to send the parameters via the Form, try to use application/x-www-form-urlencoded or multipart/form-data data type.

Refer to the following screenshot:

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.