在Express / Node服务器中通过HTTP POST接受wav音频文件 [英] Accepting a wav audio file over HTTP POST in an Express/Node server

查看:954
本文介绍了在Express / Node服务器中通过HTTP POST接受wav音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过cURL将音频文件 audio.wav 发送到我的Express服务器。我正在使用以下cURL请求:

I'm trying to send an audio file audio.wav via cURL to my Express server. I'm using the following cURL request:

 curl -X POST --data-binary @"audio.wav" -H "Content-Type: audio/wav" localhost:3000/extract_indicators/audio/darksigma

,我使用顶部的以下行:

On my server, I use the following line at the top:

app.use(bodyParser.json());

因此,我可以默认将传入请求的主体解析为JSON。在我的相应Express路由处理程序中,我有:

So that I can by default parse the body of the incoming request as JSON. In my appropriate Express routing handler, I have:

app.post('/extract_indicators/audio/:user_id', function (req, res) {
  app.use(bodyParser.raw({ type: 'audio/wav' }));
  console.log("RECIEVED AUDIO TO EXTRACT INDICATORS: ", req.body);
  <do stuff with audio and send result back>
  app.use(bodyParser.json());
});

我调用 console.log

RECIEVED AUDIO TO EXTRACT INDICATORS:  {}

我做错了什么?为什么 req.body 不包含我的数据?

What am I doing wrong? Why does req.body not contain my data?

推荐答案

这是通过使用以下声明(在 app 处理程序之外)修复的:

Turns out this is fixed by using the following declaration (outside of the app handler):

app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.raw({ type: 'audio/wav', limit: '50mb' }));

app.post('/extract_indicators/audio/:user_id', function (req, res) {
  console.log("RECIEVED AUDIO TO EXTRACT INDICATORS: ", req.body);
  <do stuff with audio and send result back>
});

控制台输出现在是:

RECIEVED AUDIO TO EXTRACT INDICATORS:  <Buffer 52 49 46 46 54 b0 01 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 01 00 80 3e 00 00 00 7d 00 00 02 00 10 00 64 61 74 61 30 b0 01 00 00 00 00 00 00 00 ... >

这篇关于在Express / Node服务器中通过HTTP POST接受wav音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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