Laravel - 使用外部请求时 POST 数据为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15672628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:39:19  来源:igfitidea点击:

Laravel - POST data is null when using external request

phplaravel

提问by Ricardo Gomes

I'm new to laravel, and I'm trying to implement a simple rest api.

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

I have the controller implemented, and tested via unit testing.

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

My problem is with the POST request.

我的问题是 POST 请求。

Via the tests Input:json has data, via an external rest client it returns null.

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

This is the code on the unit test

这是单元测试的代码

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

What am I doing wrong?

我究竟做错了什么?

UPDATE:

更新:

This is realy driving me crazy

这真的让我发疯

I've instanciated a new laravel project and just have this code:

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

Routes

路线

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

Controller:

控制器:

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 call:

卷曲调用:

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

response:

回复:

{"test":[]}

Can anyone point me to what is wrong.

任何人都可以指出我出了什么问题。

This is really preventing me to use laravel, and I really liked the concept.

这真的阻止了我使用 laravel,我真的很喜欢这个概念。

回答by Marius Ka?em?kaitis

Because you are posting JSON as your HTTP body you don't get it with Input::all(); You should use:

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

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

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

Also you can use

你也可以使用

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

instead of

代替

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

回答by M.G?kay Borulday

If you use : Route::post('test', 'XYZController@test');
Send data format : Content-type : application/json
For example : {"data":"foo bar"}

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

And you can get the post (any others:get, put...etc) data with :

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

Input::get('data');

This is clearly written in here : http://laravel.com/docs/requests. Correct Content-typeis very important!

I am not sure your CURL call is correct. Maybe this can be helpful : How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

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

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

I am using Input::get('data')and it works.

我正在使用Input::get('data')并且它有效。

回答by Rayiez

Remove header Content-type: application/jsonif you are sending it as key value pairs and not a json

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