Geon George
Geon George

Reputation: 821

How to send a POST request a file with Content-Type: application/octet-stream in Node js

I'm trying to upload something to facebook's server. Their official documentation states:

With the token from the dialog, you can submit the following call to our Graph API to submit your .zip. Note that we are using the video sub-domain, but that's intentional, since that URL is configured to receive larger uploads.

curl -X POST https://graph-video.facebook.com/{App ID}/assets 
  -F 'access_token={ASSET UPLOAD ACCESS TOKEN}' 
  -F 'type=BUNDLE' 
  -F 'asset=@./{YOUR GAME}.zip' 
  -F 'comment=Graph API upload'

I'm trying to convert this curl request to node.js using request module.

            const myzip = zipDir+file.appName+".zip"
            console.log(myzip)
            var url = "https://graph-video.facebook.com/"+file.appId+"/assets";
            const options = {
                url: url,
                headers: {
                  "Content-Type": "application/octet-stream"
                }
              }
            var req = request.post(options, function (err, resp, body) {
                console.log('REQUEST RESULTS:', err, resp.statusCode, body);
                if (err) {
                   console.log('Error!');
                  reject();
                } else {
                   console.log('URL: ' + body);
                  resolve();
                }
              });
              var form = req.form();
              var zipReadStream = fs.createReadStream(myzip,{encoding: "binary"})
              zipReadStream.setEncoding('binary')
              form.append('asset', zipReadStream);
              form.append("access_token", file.token);
              form.append("type", "BUNDLE");
              form.append("comment", mycomment)

Although I have set the headers to "Content-Type": "application/octet-stream" , I still get the error from facebook that

OAuth "Facebook Platform" "invalid_request" "(#100) Invalid file. Expected file of one of the following types: application/octet-stream"

Also when I try to log my request I get content as 'Content-Type': 'multipart/form-data and not as application/octet-stream event though I have explicitly specified this.

Upvotes: 1

Views: 16179

Answers (1)

Brad
Brad

Reputation: 163272

If you're uploading data as part of a form, you must use multipart/form-data for your Content-Type.

The Content-Type for a particular file on a form can be set per-file, but it seems that Facebook doesn't want that extra data for this.

Don't set Content-Type for your HTTP request and you should be fine, as the Request module will set it for you. Also, you can find the documentation here: https://github.com/request/request#multipartform-data-multipart-form-uploads

Upvotes: 2

Related Questions