matlab调用REST遇到的问题:Body
qq243536478 发布于2018-05 浏览:3137 回复:2
0
收藏

我用matlab调用语音识别API已经成功获得token了,但是要用发送语音的时候,我请求的body给的是int16类型的数据,header填写的是audio/pcm;rate=16000,但是运行的时候报错说:无法将 "int16" 类型且大小为 64800,1 的数据转换为 Content-Type "audio/pcm; rate=16000",或者该数据与后者不一致。请问信息的body里给的是int16类型的数组呢,还是要给别的东西呢?

%读取PCM文件
voice_file=fopen('16k.pcm');
voice_data=fread(voice_file,'int16');
sound(voice_data/max(voice_data),16e3)
fclose(voice_file);

%填写URI地址
AppID='*****************';
API_Key='*********************';
Secret_Key='**********************';
token_addr='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials';
token_host=[token_addr,'&client_id=',API_Key,'&client_secret=',Secret_Key];
uri_addr=matlab.net.URI(token_host);

%建立请求
request = matlab.net.http.RequestMessage;
request.Method=matlab.net.http.RequestMethod.POST;
request.Header=matlab.net.http.HeaderField('Content-Type', 'application/json; charset=UTF-8');
response = sendRequest(uri_addr,request);%发送并等待响应

%成功获取token
access_token=response.Body.Data.access_token;

%语音识别地址
uri_recognition=['http://vop.baidu.com/server_api?dev_pid=1536&cuid=test&token=',access_token];
uri_addr=matlab.net.URI(uri_recognition);
%建立请求
request2 = matlab.net.http.RequestMessage;
request2.Method=matlab.net.http.RequestMethod.POST;
%信息body赋值
request2.Header=matlab.net.http.HeaderField('Content-Type', 'audio/pcm;rate=16000');
request2.Body=matlab.net.http.MessageBody(int16(voice_data));

response = sendRequest(uri_addr,request2);%发送并等待响应

收藏
点赞
0
个赞
TOP
切换版块