sooon
sooon

Reputation: 4888

laravel 5.8 - postman test api request become empty

I follow this tutorial and use postman to test the api. Everything works as expected in localhost. but when I upload to the production server, somehow the api request data is filtered out.

I follow the instruction to install passport, and all that settings in various files. then I php artisan migrate at the production server, after that php artisan passport:install, then the message said successfully:

Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: <some long strings>
Password grant client created successfully.
Client ID: 2
Client secret: <some long strings>

I have a test function:

ResidentsController.php:

public function testing(Request $request){
        return $request->all();
    // return ['test'=>'success'];
    }

and route:

api.php:
Route::post('/resident/test','ResidentsController@testing');

So I am using postman to call:

POST : http://examplehost/api/resident/test/

with data:

{"name":"user001"}

and header:

Content-Type: application/json //<-- which enough for localhost

but the data return:

[] ///<--- I am expecting {"name":"user001"}

and I test with header:

[
Content-Type: application/json,
Authorization: {{'bearer'.$accessToken}} ///<-- which the direction from tutorial
]

still get back the same:

[]

I am not sure which part I have miss out. How can I resolve this?

Upvotes: 1

Views: 1104

Answers (1)

Joshua
Joshua

Reputation: 182

In Postman, click on body, select raw and select JSON(application/json) from the dropdown

In the body, insert your JSON

 {"name":"user001"} 

In your testing method

if ( $request->isJson() ) {
    $json = json_decode(trim($request->getContent()),true);
    return response()->json($json);
}

Upvotes: 1

Related Questions