React native 上载文件但设置内容类型

React native 上载文件但设置内容类型,react-native,speech-to-text,watson,React Native,Speech To Text,Watson,我让沃森从语音到文字在网上工作。我现在尝试在react native上执行此操作,但在文件上载部分出现错误 我正在使用HTTPS Watson API。我需要设置内容类型,否则Watson将返回错误响应。然而,在react native中,为了使文件上传工作,我们似乎需要将'Content-Type'设置为'multipart/form data'。在将内容类型设置为“音频/aac”时,是否仍可以在react native中上载文件 如果我设置了'Content-Type':'multipart

我让沃森从语音到文字在网上工作。我现在尝试在react native上执行此操作,但在文件上载部分出现错误

我正在使用HTTPS Watson API。我需要设置
内容类型
,否则Watson将返回错误响应。然而,在react native中,为了使文件上传工作,我们似乎需要将
'Content-Type'
设置为
'multipart/form data'
。在将内容类型设置为“音频/aac”时,是否仍可以在react native中上载文件

如果我设置了
'Content-Type':'multipart/form data'
则Watson API会给我错误信息:

{
    type: "default",
    status: 400,
    ok: false,
    statusText: undefined,
    headers: Object,
    url: "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true",
    _bodyInit: Blob,
    _bodyBlob: Blob
}
答复机构是:

{
   "code_description": "Bad Request", 
   "code": 400, 
   "error": "No JSON object could be decoded"
}
这是我的代码(完整代码在这里-):

下面是我设置“内容类型”时遇到的错误的屏幕截图:“音频/aac”:

根据要求,应:

curl -X POST -u "{username}":"{password}"
--header "Transfer-Encoding: chunked"
--form metadata="{
  \"part_content_type\":\"audio/flac\",
  \"timestamps\":true,
  \"continuous\":true}"
--form upload="@audio-file1.flac"
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
因此
内容类型
应该是
多部分/表单数据
,您可以将aac指定为
“部分内容类型”:“音频/aac”


最大的问题是
音频/aac
不受支持。您可能需要另一个编解码器。

非常感谢DanielBolanos和NikolayShmyrev,这就是我使用的解决方案:

这段代码是针对iOS的,所以我将音频录制为
blah.ulaw
,但部分内容类型是
aduio/mulaw;rate=22050
即使文件ext是
ulaw
,使用
mulaw
也是非常重要的。有趣的是:我无法在macOS桌面上播放
blah.ulaw
文件

还请注意,不能将
内容类型
设置为
多部分/表单数据
,这将破坏
边界

此外,Bluemix要求mulaw的零件内容类型中的费率

const body = new FormData();
let metadata = {
    part_content_type: 'audio/mulaw;rate=22050'  // and notice "mulaw" here, "ulaw" DOES NOT work here
};
body.append('metadata', JSON.stringify(metadata));
body.append('upload', {
    uri: `file://${file_path}`,
    name: `recording.ulaw`, // notice the use of "ulaw" here
    type: `audio/ulaw` // and here it is also "ulaw"
});

const response = await fetch('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true', {
    method: 'POST',
    headers: {
        // 'Content-Type': 'multipart/form-data' // DO NOT SET THIS!! It destroys the boundary and messes up the request
        'Authorization': `Basic ${btoa(`${USERNAME}:${PASSWORD}`)}`
    },
    body
});

噢,糟了。非常感谢@Nikolay!我会设法找到一个转换器。Bluemix似乎不支持任何iOS或Android音频格式。iOS上支持的编码:
lpcm、ima4、aac、MAC3、MAC6、ulaw、alaw、mp1、mp2、alac、amr
Android上支持的编码:
aac、aac\u eld、amr\u nb、amr\u wb、he\u aac、vorbis
。两者之间唯一的共同点是
aac
,因此我使用的是hahaHi@Noitidart,Watson STT服务确实支持其中一些编解码器,例如,它支持iOS的
ulaw
,或者支持Android的
vorbis
。请查看此链接:@DanielBolanos哦,我的天哪,太棒了!!!谢谢你和我分享,丹尼尔!我认为这些都不受支持,所以四处寻找如何从AAC转换。我会尝试一下,让你知道它是怎么回事!:)@DanielBolanos我在Android上玩得很开心,似乎有两种类型的
vorbis
ogg
webm
。我不知道安卓给了我哪一个。你是偶然知道的吗?我知道Google Chrome提供了
webm
版本,但watson不支持该版本-Hi@Noitidart,IBM watson STT服务在webm和ogg容器中都支持vorbis,因此webm+vorbis应该可以工作,如果这对您不起作用,您会遇到什么错误?来自文档():音频格式:转录免费无损音频编解码器(FLAC)、线性16位脉冲编码调制(PCM)、波形音频文件格式(WAV)、带有Opus或Vorbis编解码器的Ogg格式、带有Opus或Vorbis编解码器的网络媒体(WebM)格式、mu-law(或u-law)音频数据或基本音频。
const body = new FormData();
let metadata = {
    part_content_type: 'audio/mulaw;rate=22050'  // and notice "mulaw" here, "ulaw" DOES NOT work here
};
body.append('metadata', JSON.stringify(metadata));
body.append('upload', {
    uri: `file://${file_path}`,
    name: `recording.ulaw`, // notice the use of "ulaw" here
    type: `audio/ulaw` // and here it is also "ulaw"
});

const response = await fetch('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true', {
    method: 'POST',
    headers: {
        // 'Content-Type': 'multipart/form-data' // DO NOT SET THIS!! It destroys the boundary and messes up the request
        'Authorization': `Basic ${btoa(`${USERNAME}:${PASSWORD}`)}`
    },
    body
});