#Guys help me how to append file for formik and axios

7 messages · Page 1 of 1 (latest)

sudden marten
#

If I'm correct, you'll be receiving an empty object for images property ?

subtle mantle
#

yes formData is empty when i console log

#

but i have file in the images in formik

sudden marten
#
const formik = useFormik({
    initialValues: {
      title: "",
      description: "",
      status: 1,
      expire_at: "",
      images: null,
    },
    onSubmit: async (
      { title, description, status, expire_at, images },
      actions
    ) => {
      const formData = new FormData();
      formData.append("images", images[0]);
      formData.append("title", title);
      formData.append("description", description);
      formData.append("status", status);
      formData.append("expire_at", expire_at);

      await axios.post(
        ${process.env.NEXT_PUBLIC_BACKEND}/classified,
        formData,
        {
          headers: {
            Authorization: Bearer ${token},
          },
        }
      );
      actions.resetForm();
      setSelectedFile([]);
    },
  });
subtle mantle
#

@sudden marten i am getting this response from backend ['The images must be an array.'] what can be done?

sudden marten
#

Try formData.append("images[]", images[0])

#

If still doesn't work, would like to see the backend file handling code. what is handling the multipart requests.. like multer, or anything else