The input was not valid .Net Core Web API

52,507

Solution 1

Don't use FromBody. You're submitting as x-www-form-urlencoded (i.e. standard HTML form post). The FromBody attribute is for JSON/XML.

You cannot handle both standard form submits and JSON/XML request bodies from the same action. If you need to request the action both ways, you'll need two separate endpoints, one with the param decorated with FromBody and one without. There is no other way. The actual functionality of your action can be factored out into a private method that both actions can utilize, to reduce code duplication.

Solution 2

I just worked through a similar situation here; I was able to use the [FromBody] without any issues:

public class MyController : Controller
{
   [HttpPost]
   public async Task<IActionResult> SomeEndpoint([FromBody]Payload inPayload)
   {
   ...
   }
}

public class Payload
{
   public string SomeString { get; set; }
   public int SomeInt { get; set; }
}

The challenge I figured out was the ensure that the requests were being made with the Content-Type header set as "application/json". Using Postman my original request was returned as "The input was not valid." Adding the Content-Type header fixed the issue for me.

Solution 3

Just change [FromBody] to [FromForm].
The FromForm attribute is for incoming data from a submitted form sent by the content type application/x-www-url-formencoded while the FromBody will parse the model the default way, which in most cases are sent by the content type application/json, from the request body.
Thanks to https://stackoverflow.com/a/50454145/5541434

Solution 4

i have the same problem my solution was disable de attribute ApiController i dont know why you could read this [https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#multipartform-data-request-inference] i dont understend what is the problem

[Produces("application/json")]
    [Route("api/[controller]")]
    //[ApiController]<<remove this
    public class PagosController : ControllerBase

and you method

[HttpPost("UploadDescuento")]
public async Task<IActionResult> UploadDescuento(IEnumerable<IFormFile> files)
Share:
52,507
Souvik Ghosh
Author by

Souvik Ghosh

Updated on September 15, 2020

Comments

  • Souvik Ghosh
    Souvik Ghosh over 3 years

    I am facing a weird issue and almost spent 4 hours with no luck.

    I have a simple Web API which I am calling on form submit.

    API-

    // POST: api/Tool
    [HttpPost]
    public void Post([FromBody] Object value)
    {
        _toolService.CreateToolDetail(Convert.ToString(value));
    }
    

    HTML-

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>HTML Forms</h2>
    <form name="value" action="https://localhost:44352/api/tool" method="post">
      First name:<br>
      <input type="text" id="PropertyA" name="PropertyA" value="Some value A">
      <br>
      Last name:<br>
      <input type="text" id="PropertyB" name="PropertyB" value="Some value B">
      <br><br>
      <!--<input type="file" id="Files" name="Files" multiple="multiple"/>-->
      <br><br>
      <input type="submit" value="Submit">
    
      </form>
    </body>
    </html>
    

    When I hit the submit button I get below error-

    {"":["The input was not valid."]}
    

    Configurations in Startup class-

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddSingleton<IConfiguration>(Configuration);
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseMvc();
    }
    

    This only happens for POST request. GET request works fine. Same issue when testing in Postman REST client. Any help please? Please let me know if I can provide more details.