Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am uploading images using react native. and my API is on php based. when i am testing my API using Post Man it works perfectly. but when I pass an image from react-native app. it show me an error like "[SyntaxError: JSON Parse error: Unexpected end of input]"

What I have tried:

here I am sharing my image post method code of react native app

uploadImageToServer = () => {

    const data = new FormData();
        data.append('image', {
          name: this.state.name, 
          type: this.state.type,
          uri: this.state.uri,
     });

    fetch('https://tmbsbavan.com/api/family_method.php', {
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      body: data,
    }).then((response) => response.json())
    .then((json) => {
      console.log(json);

    }).catch((err) => { console.log(err); });
 
  }


here passed data format is like
uri = file:///data/user/0/com.tmbs_ekta/cache/rn_image_picker_lib_temp_b9855c66-9093-433e-af06-475ef6e82a3f.jpg type = image/jpeg name = rn_image_picker_lib_temp_b9855c66-9093-433e-af06-475ef6e82a3f.jpg

and here is my server-side is PHP code
<?php
    $target_dir = "profile_pic";
    $target_dir = '../'.$target_dir . "/" .rand() . "_" . time() . ".jpeg";
    
    if(move_uploaded_file($_FILES['image']['tmp_name'], $target_dir)){
        $MESSAGE = "Image Uploaded Successfully." ;
        echo json_encode($MESSAGE);
    }
?>


Please help me with how I can solve this issue, POSTMAN API testing properly works but in react native. it's showing an error
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900