Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Furrukh's avatar

React Native fetch getting an Empty array when making POST request to Laravel Api

Hi this is my first post here, sorry if I end up not following any guide lines to post here.

I am working on a project for my self learning, It is a react native app that has laravel in the backend. I have implemented the login and register screen for my apps. Now I am trying to connect it to my laravel server through its api routes. I was first having CORS issue so I solved it by making a new middleware and editing the kernel.php file as stated in this thread of stackoverflow.

https://stackoverflow.com/questions/56158474/cors-issue-with-react-app-and-laravel-api

Now I tried to run some tests with the react native app to be run on chrome and check the responses on the console. First with get request my submit function in react is

handleSubmit = async() => {
const email  = this.state.email
const pass = this.state.password


/*TEST FOR GET REQUEST */
let response = await fetch(`http://mobileprediction/api/user?email=${email}&pass=${pass}`, {
  headers: {
     "Content-Type" : "application/json",
     "Accept" : "application/json"
   },

})


let result = await response.json()
console.log(result)
}

and my api.php file in the routes of laravel was

Route::get("/user", function (Request $request) {
  return $request;
});

and I got the desired output which should have been my GET parameters

{email: "[email protected]", pass: "password"}

but then when I tested the same way with a post request I am getting an empty array no matter what and I am unable to figure out what the problem is

the handlesubmit function in my react native app is

handleSubmit = async() => {
const email  = this.state.email
const pass = this.state.password


/*TEST FOR POST REQUEST */
 let response = await fetch(`http://mobileprediction/api/user`, {
   method: "POST",
   header : {
     "Content-Type" : "application/json",
     "Accept" : "application/json"
   },
   body : JSON.stringify({
     emailid : email,
     password : pass
   }),
 })

let result = await response.json()
console.log(result)
}

and api.php file in laravel is

Route::get("/user", function (Request $request) {
  return $request;
});

Route::post("/user", function(Request $request) {
  return $request;
});

i have checked the network tab and its getting a 200 Ok status

0 likes
0 replies

Please or to participate in this conversation.