axios相当于curl –upload-file

我试图用他们的公共API上传一个gif给Gfycat。

这是在命令行上使用cURL完成的:

curl https://filedrop.gfycat.com --upload-file /tmp/name 

我试图用axios把它转换成Node.js:

 axios.post("https://filedrop.gfycat.com", { data: fs.createReadStream("/tmp/name") }).then(console.log).catch(console.log); 

但与消息的错误

 <?xml version="1.0" encoding="UTF-8"?>\n<Error><Code>PreconditionFailed</Code><Message>At least one of the pre-conditions you specified did not hold</Message><Condition>Bucket POST must be of the enclosure-type multipart/form-data</Condition><RequestId>6DD49EB33F41DE08</RequestId><HostId>/wyrORPfBWHZk5OuLCrH9Mohwu33vOUNc6NzRSNT08Cxd8PxSEZkzZdj/awpMU6UkpWghrQ1bLY=</HostId></Error> 

被打印到控制台。

我的Node.js代码如何与cURL命令不同,以及使用axios到cURL命令的等效代码是什么?

需要使用FormaData()可以使用form-data ,也可能需要使用concat-stream

 const concat = require("concat-stream") const FormData = require('form-data'); const fd = new FormData(); const fs = require('fs'); fd.append("hello", "world") fd.append("file", fs.createReadStream("/tmp/name")) fd.pipe(concat({encoding: 'buffer'}, data => { axios.post("https://filedrop.gfycat.com", data, { headers: fd.getHeaders() }) })) 

信用度: https : //github.com/mzabriskie/axios/issues/318#issuecomment-277231394