上传文件但设置Content-Type

时间:2017-05-22 07:03:23

标签: react-native speech-to-text watson

我在网络上获得了Watson Speech-to-Text。我现在尝试在本机上做反应,但在文件上传部分出现错误。

我正在使用HTTPS Watson API。我需要设置Content-Type否则Watson会返回错误响应。但是,在本机反应中,要使文件上传起作用,我们似乎需要将'Content-Type'设置为'multipart/form-data'。无论如何,在将Content-Type设置为'audio/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"
}

这是我的代码(完整代码在这里 - 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}"`);
}

以下是我设置'Content-Type': 'audio/aac'时出错的屏幕截图:

2 个答案:

答案 0 :(得分: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。您可能需要另一个编解码器。

答案 1 :(得分:1)

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

此代码适用于iOS,因此我将音频录制为blah.ulaw但是part_content_type为aduio/mulaw;rate=22050即使文件ext为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
});