2017-02-12 5 views
1

public APIを使用してGifをGfycatにアップロードしようとしています。curiosに相当する軸--upload-file

これはコマンドラインでカールを使用して行われます:私はaxiosを使ってNode.jsのにこれを変換しようと

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

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コマンドとの違いと、cURLコマンドにaxiosを使用した同等のコードは何でしょうか?

答えて

1

あなたは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

+0

私がいるFormDataのみに使用され、Node.jsのからaxiosを使用していますがブラウザのアキシャス。 – Shadowfacts