上传文件但设置 Content-Type
Upload a file but set Content-Type
我在 Web 上使用了 Watson Speech-to-Text。我现在正尝试在 React Native 上执行此操作,但在文件上传部分出现错误。
我正在使用 HTTPS Watson API。我需要设置 Content-Type
否则 Watson returns 错误响应。然而在 react-native 中,为了文件上传工作,我们似乎需要将 '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'
:
时得到的错误的屏幕截图
根据 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。您可能需要另一个编解码器。
非常感谢 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
});
我在 Web 上使用了 Watson Speech-to-Text。我现在正尝试在 React Native 上执行此操作,但在文件上传部分出现错误。
我正在使用 HTTPS Watson API。我需要设置 Content-Type
否则 Watson returns 错误响应。然而在 react-native 中,为了文件上传工作,我们似乎需要将 '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'
:
根据 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。您可能需要另一个编解码器。
非常感谢 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
});