0

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

for me it was single quotes instead of double ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.