如何在Node JS中发送POST请求的Content-Type:application/octet-stream文件 [英] How to send a POST request a file with Content-Type: application/octet-stream in Node js

查看:1844
本文介绍了如何在Node JS中发送POST请求的Content-Type:application/octet-stream文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些内容上传到Facebook的服务器.他们的官方文档指出:

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

使用对话框中的令牌,您可以将以下调用提交给我们的Graph API,以提交您的.zip.请注意,我们正在使用视频子域,但这是有意的,因为该URL已配置为可以接收较大的上载.

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'

我正在尝试使用request模块将此curl请求转换为node.js.

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)

尽管我已将标头设置为"Content-Type": "application/octet-stream",但仍然从Facebook得到错误消息

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"

当我尝试记录我的请求时,尽管我已经明确指定了此内容,但我得到的内容却是'Content-Type': 'multipart/form-data而不是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.

推荐答案

如果您将数据作为表单的一部分上传,则必须将multipart/form-data用于Content-Type.

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

可以为每个文件设置表单上特定文件的Content-Type,但是似乎Facebook不需要这样做的额外数据.

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.

不要为您的HTTP请求设置Content-Type,您应该可以,因为请求"模块会为您设置它.此外,您还可以在此处找到文档: https://github.com /request/request#multipartform-data-multipart-form-uploads

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

这篇关于如何在Node JS中发送POST请求的Content-Type:application/octet-stream文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆