Timo K
Timo K

Reputation: 27

Passing formdata in object | Axios

I'm trying to passing formdata and other values to an axios api but can't access the formData in the backend anymore:

Like this it works:

  uploadFiles(formData, schema) {
    console.log("Uploading files...", ...formData);

    return instance
      .post(`upload`, formData)
      .then(response => console.log(response.status))
  }

But trying to handle multiple parameters like this doesn't:

  uploadFiles(formData, schema) {
    console.log("Uploading files...", ...formData);

    return instance
      .post(`upload`, {
        formData,
        schema
      })
      .then(response => console.log(response.status))
  }

How would I access the formData in the backend? I can't access it like req.files anymore. And req.body.formData is empty.

Upvotes: 0

Views: 62

Answers (1)

Asimple
Asimple

Reputation: 650

As i remember axios will parse object as JSON automatically. so you can't send files in this way. You can pass schema in FormData:

uploadFiles(formData, schema) {
    console.log("Uploading files...", ...formData);
    formData.append('schema', schema);

    return instance
      .post(`upload`, formData)
      .then(response => console.log(response.status))
  }

(Posted answer from comments here)

Upvotes: 2

Related Questions