上载文件但设置了Content-Type [英] Upload a file but set Content-Type

查看:209
本文介绍了上载文件但设置了Content-Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让Watson语音转文本工作在网络上.我现在正在尝试在本机反应上执行此操作,但是在文件上传部分出现错误.

I got Watson Speech-to-Text working on the web. I am now trying to do it on react native but am getting errors on the file upload part.

我正在使用HTTPS Watson API.我需要设置Content-Type,否则Watson将返回错误响应.但是,在react-native中,为了使文件上载正常工作,我们似乎需要将'Content-Type'设置为'multipart/form-data'.将Content-Type设置为'audio/aac'时,是否仍可以在本机中上传文件?

I am using the HTTPS Watson API. I need to set the Content-Type otherwise Watson returns a error response. However in react-native, for the file upload to work, we seem to need to set 'Content-Type' to 'multipart/form-data'. Is there anyway to upload a file in react-native while setting Content-Type to 'audio/aac'?

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

The error Watson API gives me if I set 'Content-Type': 'multipart/form-data' is:

{
    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 ):

Here is my code (full code is here - 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'时出现的错误的屏幕截图:

Here is a screenshot of the error I get when I set 'Content-Type': 'audio/aac':

推荐答案

根据

According to the documentation for multipart requests the request should be:

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".

So the content-type should be multipart/form-data, you can specify aac as "part_content_type": "audio/aac".

您遇到的最大问题是audio/aac不受支持格式.您可能需要其他编解码器.

The big problem you have is that audio/aac is not in supported formats. You might probably need another codec.

这篇关于上载文件但设置了Content-Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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