0

I am sending curl request from a laravel app to express app. When i trigger curl request then it comes to my express app but i am not able to fetch any data out of it. req.body is empty. Here is the curl request which i am sending from my laravel app. $data contains array of objects.

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,"http://some.com/event_upload_manager");
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    // receive server response ...
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $server_output = curl_exec ($ch);

In my express app, code goes like this

exports.event_uploader = function(req,res){    
console.log("Curl Received");
console.log("Body : "+JSON.stringify(req.body));
console.log("Params : "+JSON.stringify(req.params));
console.log("Query : "+JSON.stringify(req.query));
}

I am getting Curl Received then empty request.

Manoj jain
  • 63
  • 1
  • 7
  • Can you add your controller method and not only the curl call? Anyway why aren't you using [Guzzle](http://docs.guzzlephp.org/en/stable/index.html) instead of the native curl? – IlGala Aug 21 '18 at 09:51
  • controller method is post request , it works fine when it fetch it in another laravel app. @IIGala – Manoj jain Aug 21 '18 at 10:33
  • Yes, but what kind of response is returning to your express app? The `$server_output` variable is returned for example with `return response()->json(['data' => $server_output]);`? – IlGala Aug 21 '18 at 10:59
  • Do you have the [`body-parser`](https://stackoverflow.com/a/12008719/7362396) middleware in your Express setup? – Tobias K. Aug 22 '18 at 18:11

1 Answers1

0

Problem is solved , i just added application/x-www-form-urlencoded as header to my curl request and it came, Besides i added json parser as a middleware for my route in express app.

Manoj jain
  • 63
  • 1
  • 7