From f2059da9d1dfdc8acd7b790acb0ab33bbf828361 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Mon, 19 May 2025 17:01:13 -0400 Subject: [PATCH] Important bug fix for posting form data. The headers are case sensitive. Changed them all to Content-Type. --- src/lib/ae_api/api_post_object.ts | 16 ++++++++++++- src/lib/ae_stores.ts | 2 +- src/lib/electron_native.js | 2 +- src/routes/+layout.ts | 2 +- .../events/ae_comp__event_files_upload.svelte | 24 +++++++++++++------ .../ae_comp__journal_obj_id_view.svelte | 4 +++- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/lib/ae_api/api_post_object.ts b/src/lib/ae_api/api_post_object.ts index b1615452..95e95059 100644 --- a/src/lib/ae_api/api_post_object.ts +++ b/src/lib/ae_api/api_post_object.ts @@ -54,15 +54,21 @@ export let post_object = async function post_object( } } + // console.log('HERE!! API POST 0'); + if (!api_cfg) { console.error('No API Config was provided. Returning false.'); return false; } + // console.log('HERE!! API POST 1'); + // Construct the URL with query parameters const url = new URL(endpoint, api_cfg['base_url']); Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); + // console.log('HERE!! API POST 2'); + // Clean the headers let headers_cleaned: Record = {}; for (const prop in api_cfg['headers']) { @@ -70,8 +76,14 @@ export let post_object = async function post_object( headers_cleaned[prop_cleaned] = api_cfg['headers'][prop]; } + // console.log('HERE!! API POST 3'); + if (form_data) { - headers_cleaned['Content-Type'] = 'multipart/form-data'; + // headers_cleaned['Content-Type'] = 'multipart/form-data'; + delete headers_cleaned['Content-Type']; + delete headers_cleaned['content-type']; // Just in case + delete headers_cleaned['Content-type']; // Just in case + console.log('Form Data:', form_data); } else { headers_cleaned['Content-Type'] = 'application/json'; } @@ -80,6 +92,8 @@ export let post_object = async function post_object( console.log('Cleaned Headers:', headers_cleaned); } + // console.log('HERE!! API POST 4'); + for (let attempt = 1; attempt <= retry_count; attempt++) { try { const controller = new AbortController(); diff --git a/src/lib/ae_stores.ts b/src/lib/ae_stores.ts index 6eddba8b..108e29ae 100644 --- a/src/lib/ae_stores.ts +++ b/src/lib/ae_stores.ts @@ -394,7 +394,7 @@ export let ae_api_data_struct: key_val = { let ae_api_headers: key_val = {}; ae_api_headers['Access-Control-Allow-Origin'] = '*'; -ae_api_headers['content-type'] = 'application/json'; +ae_api_headers['Content-Yype'] = 'application/json'; ae_api_headers['x-aether-api-key'] = ae_api_data_struct.api_secret_key; ae_api_headers['x-aether-api-token'] = 'fake-temp-token'; ae_api_headers['x-aether-api-expire-on'] = ''; diff --git a/src/lib/electron_native.js b/src/lib/electron_native.js index 4bab8047..c85f55c9 100644 --- a/src/lib/electron_native.js +++ b/src/lib/electron_native.js @@ -306,7 +306,7 @@ async function get_url_cfg(cfg) { // axios.defaults.baseURL = `${cfg.api_protocol}://${cfg.api_server}:${cfg.api_port}/${cfg.api_path}`; axios_api.defaults.headers.common['Access-Control-Allow-Origin'] = cfg.access_control_allow_origin; // '*'; // app_cfg.access_control_allow_origin; - axios_api.defaults.headers.common['content-type'] = 'application/json'; + axios_api.defaults.headers.common['Content-Type'] = 'application/json'; axios_api.defaults.headers.common['x-aether-api-key'] = cfg.api_secret_key; // axios_api.defaults.headers.common['x-account-id'] = cfg.account_id; diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index 9934aad9..4247526a 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -33,7 +33,7 @@ let ae_api_init: key_val = { let ae_api_headers: key_val = {}; ae_api_headers['Access-Control-Allow-Origin'] = '*'; -ae_api_headers['content-type'] = 'application/json'; +ae_api_headers['Content-Type'] = 'application/json'; ae_api_headers['x-aether-api-key'] = ae_api_init.api_secret_key; ae_api_headers['x-aether-api-token'] = 'fake-temp-token'; ae_api_headers['x-aether-api-expire-on'] = ''; diff --git a/src/routes/events/ae_comp__event_files_upload.svelte b/src/routes/events/ae_comp__event_files_upload.svelte index 8a53d41d..ec4ba046 100644 --- a/src/routes/events/ae_comp__event_files_upload.svelte +++ b/src/routes/events/ae_comp__event_files_upload.svelte @@ -103,7 +103,10 @@ async function handle_submit_form_files(event) { async function handle_input_upload_files(input_upload_files, task_id) { - console.log('*** handle_input_upload_files() ***'); + log_lvl = 2; + if (log_lvl) { + console.log(`*** handle_input_upload_files() *** task_id = ${task_id}`); + } const form_data = new FormData(); @@ -126,26 +129,31 @@ async function handle_input_upload_files(input_upload_files, task_id) { let endpoint = '/hosted_file/upload_files'; - console.log(form_data); + // console.log(form_data); params = null; // Uncomment and the post_promise is not seen by the "await" below // post_promise = await api.post_object({api_cfg: $cfg.api, endpoint: endpoint, params: params, data:form_data}); // Uncomment so that the post_promise is not seen by the "await" below - ae_promises.upload__hosted_file_obj = api.post_object({ + ae_promises.upload__hosted_file_obj = await api.post_object({ api_cfg: $ae_api, endpoint: endpoint, - params: params, + // params: params, form_data: form_data, task_id: task_id, - log_lvl: log_lvl + log_lvl: log_lvl, + retry_count: 1, }) .then(async function (result) { + // console.log('HERE!!', result); + // WARNING!!!! ONLY ONE FILE IS EXPECTED TO BE UPLOADED AT A TIME!!! // NOTE: The /hosted_file/upload_files endpoint will always return a list of successful files uploaded. In this case we are only uploading one file and expecting a list of one item. let x = 0; - console.log(result[x]); + if (log_lvl) { + console.log(`result = `, result[x]); + } let hosted_file_obj = result[x]; let hosted_file_id = hosted_file_obj.hosted_file_id_random; @@ -157,7 +165,9 @@ async function handle_input_upload_files(input_upload_files, task_id) { event_file_data['filename'] = hosted_file_obj.filename; event_file_data['extension'] = hosted_file_obj.extension; event_file_data['enable'] = true; - console.log(event_file_data); + if (log_lvl) { + console.log(`event_file_data = `, event_file_data); + } // $events_sess.files.new_upload_list[i].uploaded_bytes = 10; // fake 10 bytes at least... diff --git a/src/routes/journals/ae_comp__journal_obj_id_view.svelte b/src/routes/journals/ae_comp__journal_obj_id_view.svelte index 00260dd6..d266bb68 100644 --- a/src/routes/journals/ae_comp__journal_obj_id_view.svelte +++ b/src/routes/journals/ae_comp__journal_obj_id_view.svelte @@ -1,5 +1,6 @@