dongsuo9982 2013-07-30 12:53
浏览 22
已采纳

无法获得Laravel4 POST的JSON输入

I am using Laravel routes to build a RESTful API. I am routing to a Controller. In it, my function "store()" (my post function) should be getting a JSON input and I'm testing it by simply returning it. I seem to be able to see the data being passed in by doing Input::get('data') but I can't do a json_decode on that. The value of the json_decode is simply null. Can anyone help me get a working example of POSTing to a route with JSON data and accessing the data?

Here's what I have:

Route

Route::post('_api/tools/itementry/items', 'ItemEntryController');

Controller

class ItemEntryController extends BaseController
{
    //... other functions
    public function store()
    {
        if(Input::has('data'))
        {
            $x = Input::get('data');
            $data = json_decode($x);

            var_dump($data);
        }
    }
}

I'm using a REST tester client to submit a post with the following Query string parameters:

Name: "data" Value: { itemNumber:"test1", createdBy:"rsmith" }

  • 写回答

1条回答 默认 最新

  • dongyuanliao6204 2013-07-30 13:14
    关注

    This ended up being a really stupid problem. I was doing everything right, except my JSON that I was sending in the test client was formatted incorrectly. I forgot to add quotes around the key strings.

    So, instead of doing { itemNumber:"test1", createdBy:"rsmith" }

    I needed to do { "itemNumber":"test1", "createdBy":"rsmith" }

    Now it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败