我有一个用Laravel(Dingo)构建的API,它运行得很好.但是我在实现phpunit来测试我的API时遇到了问题
class ProductControllerTest extends TestCase
{
public function testInsertProductCase()
{
$data = array(
, "description" => "Expensive Pen"
, "price" => 100
);
$server = array();
$this->be($this->apiUser);
$this->response = $this->call('POST', '/products', [], [], $server, json_encode($data));
$this->assertTrue($this->response->isOk());
$this->assertJson($this->response->getContent());
}
}
Run Code Online (Sandbox Code Playgroud)
同时我的API端点指向此控制器功能
private function store()
{
// This always returns null
$shortInput = Input::only("price");
$rules = [
"price" => ["required"]
];
$validator = Validator::make($shortInput, $rules);
// the code continues
...
}
Run Code Online (Sandbox Code Playgroud)
但是它总是失败,因为API无法识别有效负载.Input :: getContent()返回JSON,但Input :: only()返回空白.进一步调查这是因为如果请求有效负载的内容类型是JSON,则Input :: only()仅返回值
那么......如何设置上面的phpunit代码以使用内容类型的application/json?我假设它必须与之有关$server但我不知道是什么
编辑:我原来的想法实际上有两个问题 …
我正在努力为Lumen + Dingo Rest API找到一个基本的工作基础,但我无法弄清楚如何将和平放在一起.
Lumen工作正常,但是当我尝试添加Dingo时,我会遇到各种各样的错误.从Dingo文档我读到:
获得程序包后,您可以在config/api.php文件或服务提供程序或引导程序文件中配置提供程序.
'jwt' => 'Dingo\Api\Auth\Provider\JWT'
Run Code Online (Sandbox Code Playgroud)
要么
app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
Run Code Online (Sandbox Code Playgroud)
我已经安装了一个新的Lumen副本,我没有看到config/api.php,所以我假设我使用这段代码放在我的bootstrap/app.php
这就是我的bootstrap/app.php样子:
<?php
require_once __DIR__.'/../vendor/autoload.php';
try {
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
$app->group(['namespace' => 'App\Api\Controllers'], function ($app) {
require __DIR__.'/../app/Api/routes.php';
});
return $app;
Run Code Online (Sandbox Code Playgroud)
运行时,我收到以下错误:
BindingResolutionException …Run Code Online (Sandbox Code Playgroud) 我是Laravel新手。我想用Laravel创建REST API,我已经使用了Dingo。现在我的问题是当我发送帖子请求时
CSRF令牌不匹配是引发错误
对于网络版本,我们使用CSRF令牌来验证请求。
谁能帮助我解决Laravel 5.1中的CSRF令牌不匹配错误。
提前致谢...
重新安装Lumen 5.2和新安装的Dingo 1.0.*@dev
我已经安装了bootstrap / app.php中提供的服务
还设置.env文件,例如
API_VERSION=v1
API_PREFIX=api
API_SUBTYPE=app
API_DEBUG=true
Run Code Online (Sandbox Code Playgroud)
在Http / routes.php中,我添加了一个测试路由,例如
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api) {
$api->get('example', 'ExampleController@test');
});
Run Code Online (Sandbox Code Playgroud)
如果我尝试,此路由将无法正常运行并在命令行中 php artisan api:routes
我得到错误
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "api:routes" is not defined.
Did you mean this?
api:docs
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?还可以使用HTTP Basic吗?
我创建了一个API,现在我尝试访问该API.邮差一切都很完美,工作正常:
但当我尝试使用guzzle/laravel代码时:
$res3 = $client3->post('https://app.EXAMPLE.com/api/update/'.$serial, [
'headers' => [
'Authorization' => 'Bearer '.$res2['token'],
'Content-Type' => 'application/json'
],
'form_params' => [
'token' => $res2['token'],
'bookingdate' => '07/07/2018 12:00 am',
'notes' => $SpecialRequests
]
]);
Run Code Online (Sandbox Code Playgroud)
我有:
宾语
data:debug:class:"Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException"file:"/ home/user_name/public_html/vendor/dingo/api/src/Auth/Auth.php"line:113
有什么问题?什么在Postman工作,但不适用于Laravel/Guzzle图书馆?
在 laravel 文档中,有一部分是关于让用户停止使用的->userFromTokenAndSecret,但它似乎不适用于 facebook,并返回一个错误,表明该方法不存在。
如何通过令牌和秘密获取用户?
我使用 Laravel 作为 API,以便它无状态地工作,并且我不使用任何重定向。
我使用 Angular 应用程序登录并发送令牌。我需要处理这个。
我刚刚删除了我安装的 Laravel 文件夹,我在其中轻松安装了 Dingo 和 JWT。Laravel 版本是 5.2.x。
现在,每当我尝试重新安装 Dingo API 时,它都会给我错误。这是我使用的命令列表:
composer create-project laravel/laravel citycare 5.2.*. (我也试过 5.1.*)composer require dingo/api:1.0.x@dev.You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: don't install dingo/api …Run Code Online (Sandbox Code Playgroud)