2017-05-22 33 views
0

我得到了Watson语音到文本在网上工作。我现在试图在原生反应上做到这一点,但在文件上传部分出现错误。上传文件,但设置内容类型

我正在使用HTTPS Watson API。我需要设置Content-Type否则Watson会返回错误响应。然而,在原生反应中,要使文件上传起作用,我们似乎需要将'Content-Type'设置为'multipart/form-data'。无论如何,在将Content-Type设置为'audio/aac'的同时上传反应本地文件?

错误沃森API给我,如果我设置'Content-Type': 'multipart/form-data'是:

{ 
    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" 
} 

这里是我的代码(完整的代码是在这里 - gist.github.com):

const ext = 'aac'; 
    const file_path = '/storage/emulated/0/Music/enter-the-book.aac'; 
    data.append('file', { 
     uri: `file://${file_path}`, 
     name: `recording.${ext}`, 
     type: `audio/${ext}` 
    }, `recording.${ext}`); 

    const response = await fetch('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true', { 
     method: 'POST', 
     headers: { 
      // 'Content-Type': `audio/${ext}`, 
      'Content-Type': 'multipart/form-data', 
      'X-Watson-Authorization-Token': token 
     }, 
     body: data 
    }); 

    console.log('watson-stt::getResults - response:', response); 

    if (response.status !== 200) { 
     const error = await response.text(); 
     throw new Error(`Got bad response "status" (${response.status}) from Watson Speach to Text server, error: "${error}"`); 
} 

下面是我在设置时得到的错误的屏幕截图:

回答

2

按照documentation for multipart requests的要求应该是:

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" 

所以content-type应该是multipart/form-data,您可以指定AAC作为"part_content_type": "audio/aac"

您遇到的最大问题是audio/aac不支持formats。您可能需要另一个编解码器。

+0

哦拍摄。非常感谢@尼古拉!我会尽力找到一个转换器。 Bluemix似乎没有支持任何iOS或Android音频格式。在iOS上支持的编码:'lpcm,ima4,aac,MAC3,MAC6,ulaw,alaw,mp1,mp2,alac,amr' Android支持的编码:'aac,aac_eld,amr_nb,amr_wb,he_aac,vorbis'。两者之间唯一的共同点就是'aac',所以我使用的是haha – Noitidart

+1

Hi @Noitidart,Watson STT服务确实支持其中的一些编解码器,例如它支持'ulaw' for iOS或'vorbis' for Android。请看这个链接:https://www.ibm.com/watson/developercloud/doc/speech-to-text/input.html –

+0

@DanielBolanos哦,我的天哪真棒!非常感谢与我分享丹尼尔!我认为这些都不被支持,所以围绕如何从AAC转换而来。将尝试这些,让你知道它是怎么回事! :) – Noitidart

1

感谢这么多的DanielBolanos和NikolayShmyrev这是我使用的解决方案:

该代码可用于iOS的,所以我录制的音频作为blah.ulaw但part_content_type是aduio/mulaw;rate=22050这是使用mulaw即使文件内线非常重要是ulaw。有趣的说明:我无法在我的macOS桌面上播放blah.ulaw文件。

另请注意,您不得将Content-Type设置为multipart/form-data这会破坏boundary

而且Bluemix需要在part_content_type率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 
}); 
相关问题