【问题标题】:Laravel - POST data is null when using external requestLaravel - 使用外部请求时POST数据为空
【发布时间】:2013-03-28 01:24:04
【问题描述】:

我是 laravel 新手,我正在尝试实现一个简单的 rest api。

我已经实现了控制器,并通过单元测试进行了测试。

我的问题在于 POST 请求。

通过测试 Input:json 有数据,通过外部 rest 客户端返回 null。

这是单元测试的代码

    $newMenu = array(
      'name'=>'Christmas Menu', 
      'description'=>'Christmas Menu',
      'img_url'=>'http://www.example.com',
      'type_id'=>1,
    );
    Request::setMethod('POST'); 
    Input::$json = $newMenu;
    $response = Controller::call('menu@index');

我做错了什么?

更新:

这真的让我发疯了

我已经实例化了一个新的 laravel 项目,并且只有以下代码:

路线

Route::get('test', 'home@index');
Route::post('test', 'home@index');

控制器:

class Home_Controller extends Base_Controller {

    public $restful = true;
    public function get_index()
    {
        return Response::json(['test'=>'hello world']);
    }
    public function post_index()
    {
        return Response::json(['test'=>Input::all()]);
    }
}

CURL 调用:

curl -H "Accept:application/json" -H"Content-type: application/json" -X POST -d '{"title":"world"}' http://localhost/laravel-post/public/test

回复:

{"test":[]}

谁能指出哪里出了问题。

这真的让我无法使用 laravel,我真的很喜欢这个概念。

【问题讨论】:

  • 你在浏览器中试过了吗?只是为了确保你的 curl 调用不是问题?
  • 我用rest客户端(CocoaRest Client)试过了,但是我还没有创建gui,所以还没有浏览器测试

标签: php laravel


【解决方案1】:

因为您将 JSON 作为您的 HTTP 正文发布,所以您无法通过 Input::all(); 获得它; 你应该使用:

$postInput = file_get_contents('php://input');
$data = json_decode($postInput, true);

$response = array('test' => $data);
return Response::json($response);

你也可以使用

Route::any('test', 'home@index');

而不是

Route::get('test', 'home@index');
Route::post('test', 'home@index');

【讨论】:

    【解决方案2】:

    如果你使用:Route::post('test', 'XYZController@test');
    发送数据格式:Content-type : application/json
    例如:{"data":"foo bar"}

    您可以通过以下方式获取帖子(任何其他:get、put...等)数据:

    Input::get('data');
    

    这里写得很清楚:http://laravel.com/docs/requests .正确的Content-type 很重要!

    我不确定您的 CURL 调用是否正确。也许这会有所帮助:How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

    我正在使用Input::get('data'),它可以工作。

    【讨论】:

      【解决方案3】:

      删除标题 Content-type: application/json 如果您将其作为键值对而不是 json 发送

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,我的帖子回复总是空的。为了解决这个问题,我将 body 键放在 guzzle 对象中,像这样

        $client = new Client([
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Authorization' => config('app.callisto_token'),
                ]
            ]);
        
            $body = [
                    'firstResult'=> 0,
                    'data' => '05/05/2022'
                ];
        
                $response = $client->post('http://'.$this->ip.'/IntegracaoERP'.'/status_pedido',
                    ['body' => json_encode($body)]
                );
        

        不要忘记正文键中的 json_encode。 希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2022-06-14
          • 2020-06-26
          • 2013-09-17
          • 1970-01-01
          • 1970-01-01
          • 2020-05-11
          • 2020-07-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多