5

here is my route.php

Route::post('users' , array('uses'  => 'Userscontroller@index'));

This is my Userscontroller.php

public function index()
    {
        $email_id = Input::get('email_id');
        $name = Input::get('name');

        return $email_id. ' ' .$name;
    }

When i run this in postman I get error as

'>' unexpected in JSON

.

Where m I missing?

Thank You.

4
  • Are you comfortable with json in core php? Commented Jun 29, 2016 at 7:13
  • Yes. I have to use Post method to get data. Commented Jun 29, 2016 at 7:14
  • Are you post normal data and want in json format, am I correct. Commented Jun 29, 2016 at 7:15
  • Here It is. { "email_id" : "[email protected]", "name" : "xyz" } Commented Jun 29, 2016 at 7:22

3 Answers 3

5

if you send json like this:

{"user":{"name":"abc","code":134}}

in laravel controller:

$data =  $request->json()->all(); //read json in request
return response()->json($data);  //send json respond
Sign up to request clarification or add additional context in comments.

1 Comment

use Content-type to your request: application/json
1

Maybe you're looking for something like this:

public function index()
{
   //if you want to get json data in your controller do this:
    Request::json()->all();

    //Return json data to your view script like this:
    return response()->json(['email_id'=>$email_id, 'name'=>$name]);
}

5 Comments

In my above question. I have mentioned my route.php and Userscontroller.php. I want data using post method in JSON Object.
There is not json data.@Bhavin Shah
Request::json()->all(); $email_id = Input::get('email_id'); $name = Input::get('name'); return response()->json_encode(['email_id'=>$email_id, 'name'=>$name]);
I have this code for getting data using post method.
dd(Request::json()->all()) what you see?
0

Try This:

 public function index(Request $request)
    {
       $dataArray = $request->get('data');
       $data = json_decode($dataArray, true);
       return 'Name: '.$data['name'].', Email: '.$data['email_id'];
    }

Send your data into data tag.

Comments

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.