How to use multipart dataon json server for react

205 views Asked by At

I am writing post method with video and images files on react. But i used multipart/form-data format for json-server but it does not work. I can't get the data I want. What is it my mistake ? Can you help me ? this is my post method.

const ApiUrl = "http://localhost:8000/";

export const sshOpenServiceRequest = async (jsonData) => {
    return axios.post(ApiUrl + `serviceList`, jsonData,
      {
        headers: {
          ContentType: "multipart/form-data",
        }
      })
    .then((response) => {
      return response;
    })
    .catch(() => {
      return false;
    });
};

this is my submit method
 const handleSubmit = async () => {
    //const isValid = checkValid();
    //if (isValid) {
      setIsSubmitting(true);

      const formData = new FormData();
      formData.append("materialCode", matCode);
      formData.append("channel", channel);
      formData.append("warehouseUnitCode", wareHouseUnit);

      for (const key in photos) {
        if (Object.hasOwnProperty.call(photos, key)) {
          const element = photos[key];
          formData.append(`images`, element);
        }
      }

      for (const key in videos) {
        if (Object.hasOwnProperty.call(videos, key)) {
          const element = videos[key];
          formData.append(`videos`, element);
        }
      }
      try {
        const result = await sshOpenServiceRequest(formData);
        console.log(result);
        if (result.data.success) {
          //history.push("/ssh-list/");
          notify("Form isteği başarılı");
        } else {
          notify("Form isteği başarısız");
        }
      } catch (error) {
        notify("Form isteği başarısız");
      }

      setIsSubmitting(true);
    //}
  };

0

There are 0 answers