Asked 6 years ago
28 Dec 2017
Views 2760
kiran

kiran posted

react-native fetch post body is not passing to server


   fetch('http://java.com/test.php?d=data',{
            method:'POST',
			 mode: 'same-origin',
    credentials: 'include',
    redirect: 'follow',
          body:JSON.stringify({username:'test',password:'test'}),  
  headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
  
        }).then((response)=>{
            if(response.status >= 200 && response.status < 300){
                return response;
            }else{
                var error = new Error(response.statusText);
                error.response = response;
                throw error;
            }
        }).then((response)=>{
            return response.json();
        }).then((data)=>{
		console.log(data);
            /* Process data */
        }).catch((error)=>{
            /* Catch Error */
        });
 


in server with php
for print_r($_REQUEST) i only found GET parameter not any POST parameter .
i tried many way but it is not working
shyam

shyam
answered Apr 25 '23 00:00

If you're experiencing issues with the body of your POST request not being passed to the server when using the fetch API in React Native, there are several things you can check.

First, make sure that you're constructing the body of your POST request properly using the JSON .stringify() method, especially if you're passing an object as the body of the request.

Next, verify that the server-side code is properly configured to receive and parse the body of the request. This typically involves using a middleware like body-parse r to parse the JSON data in the request body.

It's also a good idea to check the network request to see if the body is being sent properly. Tools like Charles Proxy or Wireshark can be used for this purpose.

Finally, make sure to check for any errors that may be preventing the request from being sent properly. You can use the catch() method on the fetch promise to log any errors that may occur.

By addressing these possible issues, you should be able to resolve the problem with the body of your POST request not being passed to the server when using the fetch API in React Native.
Post Answer