0

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.

4
  • I believe headers should be in a config object. So: config: { headers: { .... } } Commented Sep 24, 2018 at 19:08
  • Can you clarify how it does not "work"? What's the error/behavior you're seeing? Commented Sep 24, 2018 at 19:30
  • isn't the method for axios.post? I think there is something wrong with your syntax. Commented Sep 24, 2018 at 20:56
  • 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/… Commented Sep 24, 2018 at 21:30

1 Answer 1

2

Maybe this help

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

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

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.