'Should I always use FormData in react?

I´m building an application where I´ve got a login form and when I send the user data to the API I use the object of useState instead of a FormData object, Is it correct? Is there any difference between using FormData or not? Is it insecure? This is my code:

 const [datos, setDatos] = useState({
    nombre: '',
    apellidos: '',
    email: '',
    password: ''
  })

const handleSubmit = async (e)=>{
    e.preventDefault();
   await axios({
  
      url: 'http://localhost:8080/register',
      method: 'POST',
      data: datos,
    }).then(response => console.log(response)); 
    console.log(datos)
  } ```


Solution 1:[1]

No it's not a compulsion. Generally, to send multipart form data like images or some sort of files with Axios, you need to use the FormData class.

Nonetheless, if you are just sending data containing strings, numbers, or objects (as you are), then sending them in the json/object format is sufficient.

For example,

// body {firstName: 'Fred', lastName: 'Flintstone'} 

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

And yes security is handled by Axios only. Axios Is More Secure, Featuring Built In Cross Site Forgery (XSRF) Protection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Waje