D
D
Denis2021-02-18 11:40:41
Laravel
Denis, 2021-02-18 11:40:41

How to correctly accept json in laravel?

Tell me how to correctly accept Json in Laravel? I'm just starting to get to know this, I can't figure it out. Let's give json to me it's clear about how. A request came, we look in the database, if there is, we give it back. More or less like this.

$model= Model::find($id);
return response()->json($model, 200);

What about accepting Json? Now I’m testing through Postman, I can only, let’s send raw
{
    "test": 123,
    "123": 43
}

and I accept like this with
$request->all()
a regular array ,
do I need some kind of check for Json? Or how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Vasilyev, 2021-02-18
@hello_my_name_is_dany

On the lara side, you can do this:
https://laravel.com/docs/8.x/requests#retrieving-j...
But the request must have a header
Content-Type: application/json
Well, you can safely validate
https://laravel.com/docs/ 8.x/validation#validating...

N
nokimaro, 2021-02-18
@nokimaro

<?php
$request = Request::instance();
$content = $request->getContent();
$json = json_decode($content);

var_dump($json);

<?php
$content = file_get_contents('php://input');
$json = json_decode($content);

var_dump($json);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question