【发布时间】:2019-02-23 21:49:28
【问题描述】:
我是 PHP 新手。 我使用 *composer** 创建了一个 Laravel 项目。 我的控制器有两个端点 uploadFile 和 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 由 POST 操作从一个带有上传的 JSON 文件的表单调用。 我想在 uploadFile 方法中使用 Request::create(...) 和 Route::dispatch(. ..)。 testpost 被调用,但是请求的主体不是预期的。日志文件显示 $request->all() 没有返回我期望的请求正文 {"this is" : "my test content"} .
我的日志文件:
[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,
)),
)
我的代码有什么问题? API 调用或请求正文检索? 我知道我们可以直接调用 testpost 方法而不是调用 API。但是,我的最终目的是知道如何调用内部 API。
【问题讨论】:
-
这是一种非常奇怪的请求分发方法,我认为您的问题的原因是原始请求是您手动创建的重叠。
-
我希望 .Net 中有类似 HttpClient 的东西,但我找不到。
-
如果您使用的是
laravel/installer,那么您还可以在全球范围内安装GuzzleHttp/Guzzle软件包。这就是你想要使用的。文档:docs.guzzlephp.org/en/stable