0

I have this in the server side (C#, core 3.1):

[EnableCors("CorsApi")]

[Route("[action]", Name = "DeleteFile")]

[HttpPost]

public  List<string> DeleteFile(string pathFile)

{
    string commandDelete = "rm " + pathFile;

    List<string> results = ExecuteCommand(commandDelete, true);

    return results;

}

and I have this in the client side (react):

  (async () => {
    const response = await fetch('https://localhost:7198/api/Proj/DeleteFile', {
        method: 'POST',
        body: JSON.stringify({
          pathFile: srcFile,
        }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8'
        },
    })

    const data = await response.json()

    console.log(data)
})()

but the POST not work. it's work only when I send the parameter inside the url, like:

'https://localhost:7198/api/Proj/DeleteFile?pathFile=' + srcFile.

Why it's happend? And how can I fix it?

Thank You.

prog_prog
  • 371
  • 2
  • 5
  • 15

1 Answers1

0

Could you try to add [FromBody] attribute to your parameter in c# like this

public List<string> DeleteFile([FromBody] string pathFile)

This is should bind pathFile

RiotThanos
  • 48
  • 5
  • It's still not working. Returns me a 400 error (like before). ("one or more validation error occured", like the parameter is not recognized). – prog_prog Dec 15 '22 at 11:50
  • I find here the solution: https://stackoverflow.com/questions/58150652/asp-net-core-3-0-frombody-string-content-returns-the-json-value-could-not-be (last answer): Install Microsoft.AspNetCore.Mvc.NewtonsoftJson package. In your startup.cs add services.AddControllers().AddNewtonsoftJson(); – prog_prog Dec 15 '22 at 12:08