如何在 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
我正在尝试将内容上传到 facebook 的服务器。他们的官方文档指出:
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。
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)
尽管我已将 headers 设置为 "Content-Type": "application/octet-stream"
,但我仍然从 facebook 收到错误
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
事件,尽管我已经明确指定了这一点。
如果您将数据作为表单的一部分上传,则必须使用 multipart/form-data
作为您的 Content-Type
。
可以为每个文件设置表单上特定文件的 Content-Type
,但 Facebook 似乎不想要为此额外的数据。
不要为您的 HTTP 请求设置 Content-Type
,您应该没问题,因为请求模块会为您设置它。此外,您可以在此处找到文档:https://github.com/request/request#multipartform-data-multipart-form-uploads
我正在尝试将内容上传到 facebook 的服务器。他们的官方文档指出:
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。
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)
尽管我已将 headers 设置为 "Content-Type": "application/octet-stream"
,但我仍然从 facebook 收到错误
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
事件,尽管我已经明确指定了这一点。
如果您将数据作为表单的一部分上传,则必须使用 multipart/form-data
作为您的 Content-Type
。
可以为每个文件设置表单上特定文件的 Content-Type
,但 Facebook 似乎不想要为此额外的数据。
不要为您的 HTTP 请求设置 Content-Type
,您应该没问题,因为请求模块会为您设置它。此外,您可以在此处找到文档:https://github.com/request/request#multipartform-data-multipart-form-uploads