7

I'm using Vue3 and axios to post a form using FormData, but it's empty, nothing is being passed.

{"formData":{}}

Here's what I am doing:

const formData= new FormData();
formData.set("name", name);

axios.post("postform",  formData);

I also tried this:

formData.set("name", "John Doe");
axios.post("postform",  formData);

and this:

formData.append("name", "John Doe");
axios.post("postform",  formData);

But no success. There's no errors, it's just empty.

I retrieve them in PHP like this:

 echo $request->input("name");

So my question is:

How do I post data using FormData?

3
  • try adding this print_r($_REQUEST) in php rather than echo $request->input("name"); and see what shows Commented Feb 24, 2021 at 11:21
  • Thank you. It's empty. It shows an empty formData array. Commented Feb 24, 2021 at 11:23
  • are you using laravel in backend @ThiagoGuimarães? Commented Feb 24, 2021 at 12:03

3 Answers 3

4

If you are not sending any file (or your form is not maltipart/form-data) you can use following method to send data through axios:

let formData = {};
formData.name =  "John Doe";
axios({
  url: "postform",
  method: "POST",
  data: formData
});

EDIT:

If you are sending any files then in my case following worked for me:


const formData = new FormData();
formData.append("name", "hello world");



axios({
    url: 'postform',
    method: "POST",
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you but I need to send files.
@ThiagoGuimarães I have edited the answer please check
Doesn't work but thank you. It says getHeaders doesn't exist on type formData
@ThiagoGuimarães i have edited the answer (added 'Content-Type': 'multipart/form-data') in headers. Try that
Thank you but I it was actually my fault. I had formData inside {} in axios. But I upvoted your answer for taking your time. Thank you once again
3

You can convert your FormData Object to simple object and then send that object to server.

const formData = new FormData();
formData.set("key1", "value1");
formData.set("key2", "value2");

let data = {};

// convert the key/value pairs
for(var pair of formData.entries()) {
    data[pair[0]] = pair[1];
}

axios.post("postform",  data);

2 Comments

Thank you. I will upvote because I could see the values but I'm still getting some errors when trying to pass the value to the key. Type 'FormDataEntryValue' is not assignable to type 'string'
FormData entry value may be either string, null or File. Try formData.get('foobar').toString()
0

You might need to send the header 'Content-Length': formData.getLengthSync(). This call works for me:

const response = await axios.delete(url, {
  "data": formData,
  "headers": {
    "Content-Length": formData.getLengthSync()
  }
});

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.