如何在 php artisan 中发出测试 POST 请求?

Gri*_*dov 1 laravel laravel-5.2

我需要发出 POST 请求,我可以通过 PHP artisan 来完成吗?

我尝试使用GuzzleHttp,但出现错误:

ServerException in RequestException.php line 107:
Server error: `POST http://localhost/public/api/order` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex,nofollow" />
<style>
(truncated...)
Run Code Online (Sandbox Code Playgroud)

那么,工匠可以吗?

And*_*lan 8

不确定我的方法有多有效,但这是我过去运行过的一个例子。

  • 进入项目目录。

  • 类型 php artisan tinker

  • 创建要传递的参数数组:

$params = ['paramOne' => 'valueOne', 'paramTwo' => 'valueTwo'];

  • 创建要发送的请求的新实例。默认请求类型为:

$request = Illuminate\Http\Request::create($uri, $method, $params);

  • 如果您从 Illuminate 创建了自己的请求类,则自定义请求可能是这样的:

$request = App\Http\Requests\MyExtendedRequest::create($uri, $methodType, $params);

$uri是您要发送请求的位置 $methodType是要使用的 HTTP 方法的类型(GET、POST、PUT 等)

  • 创建处理请求的控制器

$controller = new App\Http\Controllers\MyController;

  • 提交请求。

$response = $controller->storeMission($request);