Sha*_*cts 2 javascript curl node.js axios
我正在尝试使用他们的公共 API将 gif 上传到 Gfycat 。
这是在命令行上使用 cURL 完成的:
curl https://filedrop.gfycat.com --upload-file /tmp/name
Run Code Online (Sandbox Code Playgroud)
我尝试使用axios将其转换为 Node.js :
axios.post("https://filedrop.gfycat.com", {
data: fs.createReadStream("/tmp/name")
}).then(console.log).catch(console.log);
Run Code Online (Sandbox Code Playgroud)
但消息错误
<?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>
Run Code Online (Sandbox Code Playgroud)
打印到控制台。
我的 Node.js 代码与 cURL 命令有何不同,使用 axios 到 cURL 命令的等效代码是什么?
您需要使用can useFormaData()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()
})
}))
Run Code Online (Sandbox Code Playgroud)
信用:https : //github.com/mzabriskie/axios/issues/318#issuecomment-277231394
| 归档时间: |
|
| 查看次数: |
2797 次 |
| 最近记录: |