简体   繁体   中英

Can't get JSON input for Laravel4 POST

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" }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM