Skip to content

Commit 564d690

Browse files
committed
API client now supports multipart/form-data as well
1 parent 49213c6 commit 564d690

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/ApiClient.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class ApiClient {
2020
pathParams?: Record<string, string>,
2121
queryParams?: Record<string, any>,
2222
body?: any,
23+
requestContentType?: string,
2324
) => {
2425
queryParams = queryParams ?? {};
2526
queryParams.api_key = this.apiConfig.apiKey;
@@ -32,22 +33,38 @@ export class ApiClient {
3233

3334
url += `?${encodedParams}`;
3435
const clientRequestId = uuidv4();
35-
const headers = {
36+
const headers: Record<string, string> = {
3637
Authorization: this.apiConfig.token,
3738
'stream-auth-type': 'jwt',
38-
'Content-Type': 'application/json',
3939
'X-Stream-Client': 'stream-node-' + process.env.PKG_VERSION,
4040
'Accept-Encoding': 'gzip',
4141
'x-client-request-id': clientRequestId,
4242
};
4343

44+
// https://stackoverflow.com/questions/39280438/fetch-missing-boundary-in-multipart-form-data-post
45+
if (requestContentType !== 'multipart/form-data') {
46+
headers['Content-Type'] = requestContentType ?? 'application/json';
47+
}
48+
4449
const signal = AbortSignal.timeout(this.apiConfig.timeout);
4550

51+
const encodedBody =
52+
requestContentType === 'multipart/form-data'
53+
? new FormData()
54+
: JSON.stringify(body);
55+
if (requestContentType === 'multipart/form-data') {
56+
Object.keys(body).forEach((key) => {
57+
(encodedBody as FormData).append(key, body[key]);
58+
});
59+
}
60+
61+
console.log('encodedBody', encodedBody);
62+
4663
try {
4764
const response = await fetch(`${this.apiConfig.baseUrl}${url}`, {
4865
signal,
4966
method,
50-
body: JSON.stringify(body),
67+
body: encodedBody,
5168
headers,
5269
dispatcher: this.dispatcher,
5370
});

0 commit comments

Comments
 (0)