0
votes

I need to send file via axios POST request to a remote API with the following parameter : 'operation' => 'x' and 'files' => the file itself.

The file is a pdf file. I tried this:

let formData = new FormData();

formData.append('operation', 'x');

formData.append('files', this.file);

axios({

  url: 'http://xxxxx',

  method: 'POST',

  data: formData,

  headers: {

    Accept: 'application/json',

    'Content-Type': 'multipart/form-data'

     }

    }).then(response => {

      console.log(response);

    })

    .catch(error => {

      console.log(error);

    });

}

But I cannot get it work. I trieed also using set method on formData but it doesn't work aswell. Is there something I am making wrong? PS. I tried with POSTMan and it works so it is not an API problem. Thank you.

1
I believe headers should be in a config object. So: config: { headers: { .... } }Jayem163
Can you clarify how it does not "work"? What's the error/behavior you're seeing?tony19
isn't the method for axios.post? I think there is something wrong with your syntax.Juan Carlos Eduardo Romaina Ac
what, exactly, is this.file? The file needs to be a file form element previously selected by the user, look at this example github.com/axios/axios/blob/master/examples/upload/…tato

1 Answers

2
votes

Maybe this help

axios.post(url, {params: {}}).then().catch()