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

Getting 400 from Axios Post request

I have a dotnet core webapi backend and on front end react typescript. I have one endpoint in which I create a user, but when I am submitting a response, I get 400 bad request errors on the browser console. For detail, I saw in the network tab it’s saying: errors: {Name: ["The Name field is required."]}

Here is my Axios post request:

const res = await axios.post(
            `$localhost:5000/api/User`, {
              body: {Name: "test", Username: "testuser" },
              headers: { "Content-Type": "application/json" },
          }

on back-end

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

public async Task<ActionResult> Post([FromBody] UserDto dto)
{

}

UserDto contains three properties one "Age" is optional two are required "Name" and "Username"

the same request use Post man is working but not working through Axios

>Solution :

As per the docs, when using the alias methods url, method, and data properties don’t need to be specified in config.

Please try

axios.post(
  `$localhost:5000/api/User`, // url
  { Name: "test", Username: "testuser" }, // data or body
  {
    headers: { "Content-Type": "application/json" } // config
  }
)
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