【问题标题】:Unable to fetch data from curl request in my express app?无法从我的快速应用程序中的 curl 请求中获取数据?
【发布时间】:2018-08-21 08:46:05
【问题描述】:

我正在从 laravel 应用程序向 express 应用程序发送 curl 请求。当我触发 curl 请求时,它会出现在我的 express 应用程序中,但我无法从中获取任何数据。 req.body 为空。这是我从我的 laravel 应用程序发送的 curl 请求。 $data 包含对象数组。

                    $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);

在我的 express 应用中,代码是这样的

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));
}

我收到 Curl,然后是空请求。

【问题讨论】:

  • 你能添加你的控制器方法而不仅仅是 curl 调用吗?无论如何,您为什么不使用 Guzzle 而不是原生 curl?
  • 控制器方法是 post request ,当它在另一个 laravel 应用程序中获取它时它工作正常。 @IIGala
  • 是的,但是您的快递应用会返回什么样的响应? $server_output 变量返回例如 return response()->json(['data' => $server_output]);?
  • 您的 Express 设置中有 body-parser 中间件吗?

标签: node.js laravel express curl


【解决方案1】:

问题已解决,我刚刚将 application/x-www-form-urlencoded 作为标头添加到我的 curl 请求中,它来了, 此外,我在 express 应用中添加了 json 解析器作为我的路由的中间件。

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多