I am a newbie in PHP. I created a Laravel project using *composer**. My controller has two endpoint uploadFile and testpost:
public function uploadFile(Request $request) {
//there are more code about reading uploaded file here. Everything is OK here.
$request = Request::create('/api/testpost', 'POST',
[],[],[],[],'{"this is" : "my test content"}');
return Route::dispatch($request);
}
public function testpost(Request $request){
Log::info($request->all());
return response()->json(["title"=>"this is the test get method"]);
}
uploadFile is invoked by POST action from a form which carries an uploaded JSON file. I want to call testpost inside of uploadFile method using Request::create(...) and Route::dispatch(...). testpost is invoked however the body of request is not as expected. The log file shows me that $request->all() does not return the request body which I expect to be {"this is" : "my test content"}.
My log file:
[2019-02-23 12:16:47] local.INFO: array (
'_token' => 'JzQjclRD4WaTkezqLxlU48D1dM7S3X2X3hok3kr4',
'employee_file' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'test_input_file.txt',
'mimeType' => 'text/plain',
'error' => 0,
'hashName' => NULL,
)),
)
What wrong in my code? API invocation or request body retrieval? I know that we can call testpost method directly instead of calling API. However, I ultimate purpose is to know how to call an internal API.
laravel/installerthat you also haveGuzzleHttp/Guzzlepackage installed globally. That's what you'd want to use. Docs: docs.guzzlephp.org/en/stable