I'm developing a plugin for Obsidian.md that needs to make an HTTP request to an API that expects a file in a multipart/form-data
payload. In order to ensure the plugin works on mobile, I have to use the requestUrl
method provided by Obsidian's TypeScript lib in order to ensure the plugin will work on mobile, among some other compatibility reasons.
However, requestUrl
expects the body to be a string
or an ArrayBuffer
- is there a way to convert FormData
into a string
or ArrayBuffer
? Or am I taking the wrong approach to constructing the payload?
The interface: 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;
}
My code:
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);
});
Current error:
Type 'FormData' is not assignable to type 'string | ArrayBuffer | undefined'.
Additional References: API Repo API Docs A working implementation of the method