Important bug fix for posting form data. The headers are case sensitive. Changed them all to Content-Type.

This commit is contained in:
Scott Idem
2025-05-19 17:01:13 -04:00
parent f88e6cef89
commit f2059da9d1
6 changed files with 38 additions and 12 deletions

View File

@@ -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<string, string> = {};
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();

View File

@@ -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'] = '';

View File

@@ -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;

View File

@@ -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'] = '';

View File

@@ -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...

View File

@@ -1,5 +1,6 @@
<script lang="ts">
// *** Import Svelte specific
import { goto } from '$app/navigation';
// *** Import other supporting libraries
import {
@@ -141,7 +142,8 @@ function verify_journal_passcode() {
<button
type="button"
onclick={() => {
// $journals_sess.show__modal_new__journal_entry_obj = true;
// $journals_sess.show__modal_new__journal_entry_obj = true
// log_lvl = 3;
let data_kv = {
category_code: null,