1

I can't append an object to FormData

I use Axios

I don't want to send data by JSON.stringify()

data () {
   return {
      product: {

          title: '',

          description: '',

          properties: {
             property1: '',
             property2: ''
          }
      }
   }
}

I want this

‍‍

{title: '', description: '', properties:{property1: '', property2: ''}}

1 Answer 1

4

Try using Object.entries. For example...

// If this is the object you want to convert to FormData...
const item = {
    description: 'First item',
    price: 13,
    photo: File
};

const formData = new FormData();

Object.entries(item).forEach(([key, value]) => {
    formData.append(key, value);
});

// At this point, you can then pass formData as the payload to axios

Read more about Object.entries() over here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.