What is the axios or ajax equivalent for cURL's --form?

Viewed 882

I am creating a task to send a PUT request and upload a .pot file using axios in node.

I can run

curl -i -u api:<api-key> -F file=@dist/file.pot https://api-link.com/v2/api 

and it'll work fine

I have tried doing this in node

var fd = require('form-data');
var axios = require('axios');
var form = new FormData();
form.append('file', 'dist/file.pot');
var header = { headers: Object.assign({}, apiToken(), form.getHeaders()) };

axios.put('https://api-link.com/v2/api/', form, header)
    .then(function(res) {
        console.log(res);
    })
    .catch(function(err) {
        console.log(err);
    })

and this doesn't work like the curl command. apiToken() is a function to handle the -u api:<api-key> part of the curl command, and it returns {'Authorization': 'Basic <api-key>'}. I've checked that the function works because I am able to successfully do a get request.

So what is the equivalent of -F file=@dist/file.pot in ajax or axios?

1 Answers

Try the below solution:

var options = { headers: Object.assign({'Content-Type': 'multipart/form-data'}, apiToken(), form.getHeaders()) };

axios.post('https://api-link.com/v2/api/', formData, options)

Also check if the content-type header is being set as multipart/form-data or not.

Related