我正在为Obsidian.md开发一个插件,它需要向需要multipart/form-data
负载中的文件的API发出HTTP请求。为了确保插件在移动的上工作,我不得不使用requestUrl
方法provided by Obsidian's TypeScript lib,以确保插件在移动设备上工作,以及其他一些兼容性原因。
然而,requestUrl
期望主体是string
或ArrayBuffer
-有没有办法将FormData
转换为string
或ArrayBuffer
?还是我在构建有效载荷时采取了错误的方法?
接口:https://marcus.se.net/obsidian-plugin-docs/reference/typescript/interfaces/RequestUrlParam
/** @public */
export interface RequestUrlParam {
/** @public */
url: string;
/** @public */
method?: string;
/** @public */
contentType?: string;
/** @public */
body?: string | ArrayBuffer;
/** @public */
headers?: Record<string, string>;
/**
* Whether to throw an error when the status code is >= 400
* Defaults to true
* @public
*/
throw?: boolean;
}
我的代码:
const formData = new FormData();
const data = new Blob([await this.app.vault.adapter.readBinary(fileToTranscribe.path)]);
formData.append("audio_file", data);
const options: RequestUrlParam = {
method: "POST",
url: "http://djmango-bruh:9000/asr?task=transcribe&language=en",
contentType: "multipart/form-data",
body: formData,
};
requestUrl(options).then((response) => {
console.log(response);
}).
catch((error) => {
console.error(error);
});
当前错误:Type 'FormData' is not assignable to type 'string | ArrayBuffer | undefined'.
其他参考文献:API RepoAPI DocsA working implementation of the method
2条答案
按热度按时间oipij1gg1#
我已经建立了一个解决方案,本质上我只是检查了来自fetch/Postman的有效请求,现在正在以相同的格式手动构建multipart/form-data有效负载。
可能有一个现有的库或util函数以更清晰的方式完成此操作,如果我没有找到一个,我可以自己编写一个,如果我需要在插件的其他部分发送文件负载请求。
全源here
emeijp432#
如果您不想滚动自己的文件,
multi-part-lite
库似乎可以很好地完成此任务:输出: