#FormData uploading

3 messages · Page 1 of 1 (latest)

carmine lake
#

Hello,

I need assistance in modifying the code to send a POST request using FormData for uploading photos to the server. I have the following function:

const _upload = async (file, name, caption, alternativeText) => {
  try {
    const data = {
      fileInfo: JSON.stringify({
        alternativeText,
        caption,
        name,
      }),
      files: { file, content_type: mime.lookup(file) },
    };
    const { body } = await needle("post", strapiUrl + "/api/media", data, {
      multipart: true,
      headers: {
        authorization: _axios.defaults.headers.Authorization,
      },
    });
    return Array.isArray(body) ? (body.length > 0 ? body[0] : null) : body;
  } catch (e) {
    console.error(`File upload error: ${e.message}`);
    return null;
  }
};

I want to refactor this function to use FormData for sending the POST request. How can I do this correctly? Thank you!

iron hound
#

Duplicate thread, please use the other one