From d10f22b603af068c5de486a9740a7f4671637d7d Mon Sep 17 00:00:00 2001 From: steve192 Date: Tue, 12 Mar 2024 00:35:43 +0100 Subject: [PATCH] fix: image upload on android --- src/components/RecipeImageViewPager.tsx | 4 ++-- src/dao/RestAPI.ts | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/components/RecipeImageViewPager.tsx b/src/components/RecipeImageViewPager.tsx index 455c26f..3375a84 100644 --- a/src/components/RecipeImageViewPager.tsx +++ b/src/components/RecipeImageViewPager.tsx @@ -34,8 +34,8 @@ export const RecipeImageViewPager = (props: Props) => { props.onImageAdded?.(uuid); }).catch((error) => { // TODO: Error handling - console.error(error); - alert('Error uploading picture' + error); + console.error(error, JSON.stringify(error)); + alert('Error uploading picture' + JSON.stringify(error)); }); }; diff --git a/src/dao/RestAPI.ts b/src/dao/RestAPI.ts index 6fd212b..2d25af3 100644 --- a/src/dao/RestAPI.ts +++ b/src/dao/RestAPI.ts @@ -329,7 +329,7 @@ class RestAPI { } - const response = await this.post('/recipes-images', formData); + const response = await this.post('/recipes-images', formData, {'Content-Type': 'multipart/form-data'}); return response?.data.uuid; } static async getRecipeById(recipeId: number): Promise { @@ -345,9 +345,10 @@ class RestAPI { }); } - static async axiosConfig(): Promise { + static async axiosConfig(headers?: {[headerName: string]: string}): Promise { + const mergedHeaders = {...await this.getAuthHeader(), ...headers}; return { - headers: await this.getAuthHeader(), + headers: mergedHeaders, }; } @@ -401,9 +402,9 @@ class RestAPI { return await AppPersistence.getBackendURL() + AppPersistence.getApiRoute() + path; } - private static async post(apiPath: string, data: any) { + private static async post(apiPath: string, data: any, headers?: {[headerName: string]: string}) { try { - return await axios.post(await this.url(apiPath), data, await this.axiosConfig()); + return await axios.post(await this.url(apiPath), data, await this.axiosConfig(headers)); } catch (e) { await RestAPI.handleAxiosError(e); // Retry after error handling