WhiteRau
WhiteRau

Reputation: 857

JS: axios POST with large nested object and form-data

I am to post an Axios request because using get results in a 414 error. Here's the object:

rows= {
  0 : {
    "name":"Thor",
    "status":"active",
    "email":"[email protected]",
  },
  1 : {
    "name":"Mesa",
    "status":"active",
    "email":"[email protected]",
  },
  2 : {
    "name":"Jesper",
    "status":"stdby",
    "email":"[email protected],
  },
}

This is just a sample of the object's format. There is 400+ elements in the real one, thus post instead of get. I am having trouble properly building the form-data on this one. Here's what I have:

let data = new FormData();
Object.keys(rows).forEach(key => data.append(key, rows[key]));  //  <--- this doesn't do
data.set('target', target);  //  <---- this comes through just fine

axios({
  method: 'post',
  url: 'byGrabthorsHammer.php',
  data: data,
  headers: {'Content-Type': 'multipart/form-data'}
}).then(function(response) {
  if (response.error) {
    console.log('failed to send list to target');
    console.log(response);
  } else {
    console.log('response: ');
    console.log(response);
  }        
});

What comes through is just [Object][Object]' when ivar_dump($_POST);`. This is not what I want. How could I rewrite this properly so I get the data to the other side (like GET...).

Upvotes: 0

Views: 7254

Answers (2)

Ernesto
Ernesto

Reputation: 4252

Yow bro, POST Are for inserting new stuff, instead of doing a post you need a patch axios.patch it is basically the same. And it won’t fix your issue. To fix the issue you need to set the Content-Type to application/json, then on yow

axios.post(url, data: JSON.stringify(bigObject))
   .then(Rea=>Rea)

Upvotes: 2

Mike
Mike

Reputation: 96

You could try stringifying the data. JSON.stringify(data)

Upvotes: 1

Related Questions