如何在 Node js 中使用 Content-Type: application/octet-stream 发送 POST 请求文件 [英] How to send a POST request a file with Content-Type: application/octet-stream in Node js
问题描述
我正在尝试将某些内容上传到 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-输入
.
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 中使用 Content-Type: application/octet-stream 发送 POST 请求文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!