How to get json post request in laravel?

Member

by denis , in category: PHP Frameworks , a month ago

How to get json post request in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a month ago

@denis 

To get a JSON POST request in Laravel, you can use the request() helper function or the Input facade to retrieve the JSON data from the request. Here is an example of how you can do this in a controller:

1
2
3
4
5
6
public function store(Request $request)
{
    $data = $request->json()->all();

    // You can now access the JSON data through the $data variable
}


Alternatively, you can also use the input() method on the request object to retrieve specific JSON values like so:

1
2
3
4
5
6
7
public function store(Request $request)
{
    $name = $request->input('name');
    $email = $request->input('email');

    // You can now access the individual JSON values
}


Make sure that the request is being sent with the correct Content-Type header set to application/json for Laravel to properly detect and parse the JSON data in the request.