Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

djamiirr's avatar

$request->getContent() isn't an objcet

Hi Laracasts community. I'm using Laravel v6 for an API development and I'm facing a problem. In a post method, when i pass a json object with postman, the $request->getContent() contains a srtring value not a json.

Here is my controller method:

function updateEmployee(Request $request){ $request = $request->getContent(); dd($request); }

api.php:

Route::group(['middleware' => ['auth:api']], function () { Route::get('/employee/{id?}', 'UserController@index')->middleware('scope:admin, normal')->where('id', '[0-9]*'); Route::post('/employee', 'UserController@updateEmployee')->middleware('scope:admin'); });

Request headers are: Authorization: Bearer..., Content-type: Application/json, Accept: Application/json

I tried with json_decode($request->getContent()) but no luck.

any Solution? And thanks.

0 likes
7 replies
ftiersch's avatar

What do you mean "a string not a json"? JSON is a string before you decode it. What does it contain?

djamiirr's avatar

here is the dd($request->getContent()) output:

"""\n {\n \t"email": "[email protected]",\n \t"test": "test",\n \t"last_name": "test",\n \t"finger_print": 0036,\n \t"started_mounth": 01,\n \t"started_year": 2020,\n \t"filiale_id": 1,\n \t"usr_type": 1,\n \t"blood_id": 3,\n \t"country_id": 222,\n \t"phone_country_id": 222,\n \t"land_phone_country_id": 222,\n \t"title": "Fullstack Developer",\n \t"id": 6\n }\n """

so ican't for example perform $request->getContent()['email'] or $request->getContent()->email

UPDATE: $request->all() returns an empty array

djamiirr's avatar

Tnaks but i'm already using the api.php router

djamiirr's avatar

Solved, the json was not valid, after json correction i becomes able to perform json_decode($request->getContent())

Spiral's avatar
$rule = [
            'file' => 'required|file|required|mimes:doc,docx,txt,json'
        ];
        
        if(!$this->validateRequest($request, $rule)) {
            return response()->json($this->response, $this->responseCode);
        }                

        $file = $request->file->storeAs('importFiles', $request->file->getClientOriginalName());

        $getFile = Storage::disk('local')->get($file);
                
        $data = json_decode($getFile,true);
Spiral's avatar
	$file= file_get_contents(public_path(file.json'));
        $objs = json_decode($file,true);

Please or to participate in this conversation.