简体   繁体   English

无法使用NodeJS上传音频文件

[英]Can't upload audio file with NodeJS

I have a working piece of python: 我有一个工作的蟒蛇:

url = "abc.com"
data = open('speech.wav', 'rb').read()
res = requests.post(url=url,
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})

which allows me to upload a wav file as a post request to the server. 这使我可以将wav文件作为发布请求上传到服务器。 However, the below NodeJS code 但是,下面的NodeJS代码

var req = request.post('abc.com', function(err, resp, body) {
    if (err) {
        console.log('Error!');
    } else {
        console.log('URL: ' + body);
    }
});
var form = req.form();
form.append('Content-Type', 'application/octet-stream');
form.append('data', fs.createReadStream('speech.wav'));

returns "Unsupported Media Type: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." 返回“不支持的媒体类型:服务器拒绝此请求,因为请求实体的格式不受请求的方法所请求的资源支持。”

What am I doing wrong on Node? 我在Node上做错了什么?

Try to send content-type as header, rather than in form. 尝试将content-type作为标头而不是形式发送。 The content-type in form is considered for multipart form data. 表单中的内容类型用于多部分表单数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM