0

I’m trying to post an item to my server. I’m using React Native for my front end and Laravel as my back-end. I have tried it many times but it keeps giving me Network request failed. Though I am trying it on postman and it works fine.

this is my demo code:

var dummyArray = [
                { 
                type: "image/jpeg", 
                uri: "file:///storage/emulated/0/Android/data/com..../files/Pictures/4cbc8fa0-5978-42a6-89ab-ea9639cb43e4.jpg", 
                name: ("file:///storage/emulated/0/Android/data/com.../files/Pictures/4cbc8fa0-5978-42a6-89ab-ea9639cb43e4.jpg").split("/").pop() 
                },
                { 
                    type: "image/jpeg", 
                    uri: "file:///storage/emulated/0/Android/data/com..../files/Pictures/e3de766a-5c67-4926-9a56-93b0c989970a.jpg", 
                    name: ("file:///storage/emulated/0/Android/data/com..../files/Pictures/e3de766a-5c67-4926-9a56-93b0c989970a.jpg").split("/").pop() 
                }
            ]

 const formdata = new FormData();
 formdata.append('images[]', this.state.imageArray);
1
  • you are trying it on postman and it works fine. that means server working fine. so may be you have some errors in react-native code or in connection Commented Nov 18, 2021 at 6:43

3 Answers 3

1

I have resolve similar and like below:

submitImage(images = [{ uri: "", fileName: "", type: "" }]) {
    const oFormData = new FormData();
    images.map((val) => {
        oFormData.append("image", {
            uri: val.uri,
            type: val.type,
            name: val.fileName
        });
    });
    return axios.post(PostServiceUrl, oFormData);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are passing whole array as a single param which is wrong. You should add all the images one by one in form data

 let formData = new FormData();
        if (images.length > 0) {
          images.forEach((element) => {
            formData.append('images', element);
          });
        } else {
          formData.append('images', undefined);
        }

Comments

0

i have resolved my query with following code:

    Object.keys(dummyArray).forEach((item, i) => {
                                    
 formdata.append(`images[${item}]`, 
     {
          uri: dummyArray[item].uri,
          name: dummyArray[item].name,
          type: dummyArray[item].type
    }
 );
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.