json rpc 2.0 在 laravel api 添加请求时出错
json rpc 2.0 in laravel api errors on add request
我加 https://github.com/sajya/server
对我的 laravel 8 进行 json rpc。
我只是按照此处提供的说明进行测试 https://sajya.github.io/docs/
所有基本示例都适用,但如果我将参数添加到 curl 字符串,则会引发错误,我无法理解:
我的代码:
<?php
declare(strict_types=1);
namespace App\Http\Procedures;
use Sajya\Server\Procedure;
use Illuminate\Http\Request;
class PharmaciesProcedure extends Procedure
{
/**
* The name of the procedure that will be
* displayed and taken into account in the search
*
* @var string
*/
public static string $name = 'pharmacies';
public function ping(Request $request)
{
return $request->input('innings');
}
}
我的路线api文件:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Procedures\PharmaciesProcedure;
Route::rpc('/test1', [PharmaciesProcedure::class])->name('rpc.endpoint');
此请求有效:
curl 'http://127.0.0.1:8000/api/test1' --data-binary
'{"jsonrpc":"2.0","method":"pharmacies@ping","params":[],"id" : 1}'
这不是:
curl 'http://127.0.0.1:8000/api/test1' --data-binary
'{"jsonrpc":"2.0","method":"pharmacies@ping","params":["innings": "out"],"id" : 1}'
错误:
{"id":null,"error":{"code":-32700,"message":"Parse
error","data":null,"file":"/home/natty/workspace/farmacie/vendor/sajya/server/src/Http/Parser.php","line":133,"trace":"#0
/home/natty/workspace/farmacie/vendor/sajya/server/src/Http/Parser.php(104):
Sajya\Server\Http\Parser->checkValidation()\n#1
/home/natty/workspace/farmacie/vendor/sajya/server/src/Guide.php(49):
Sajya\Server\Http\Parser->makeRequests()\n#2
/home/natty/workspace/farmacie/vendor/sajya/server/src/JsonRpcController.php(27):
Sajya\Server\Guide->handle()\n#3
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(48):
Sajya\Server\JsonRpcController->__invoke()\n#4
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(255):
Illuminate\Routing\ControllerDispatcher->dispatch()\n#5
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197):
Illuminate\Routing\Route->runController()\n#6
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691):
Illuminate\Routing\Route->run()\n#7
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128):
Illuminate\Routing\Router->Illuminate\Routing\{closure}()\n#8
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#9
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Routing\Middleware\SubstituteBindings->handle()\n#10
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#11
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(103):
Illuminate\Routing\Middleware\ThrottleRequests->handleRequest()\n#12
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(55):
Illuminate\Routing\Middleware\ThrottleRequests->handleRequestUsingNamedLimiter()\n#13
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Routing\Middleware\ThrottleRequests->handle()\n#14
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#15
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693):
Illuminate\Pipeline\Pipeline->then()\n#16
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668):
Illuminate\Routing\Router->runRouteWithinStack()\n#17
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634):
Illuminate\Routing\Router->runRoute()\n#18
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623):
Illuminate\Routing\Router->dispatchToRoute()\n#19
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166):
Illuminate\Routing\Router->dispatch()\n#20
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128):
Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}()\n#21
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#22
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()\n#23
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#24
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()\n#25
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#26
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle()\n#27
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#28
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle()\n#29
/home/natty/workspace/farmacie/vendor/fruitcake/laravel-cors/src/HandleCors.php(57):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#30
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Fruitcake\Cors\HandleCors->handle()\n#31
/home/natty/workspace/farmacie/vendor/fideloper/proxy/src/TrustProxies.php(57):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#32
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167):
Fideloper\Proxy\TrustProxies->handle()\n#33
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#34
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141):
Illuminate\Pipeline\Pipeline->then()\n#35
/home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110):
Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()\n#36
/home/natty/workspace/farmacie/public/index.php(52):
Illuminate\Foundation\Http\Kernel->handle()\n#37
/home/natty/workspace/farmacie/server.php(21):
require_once('/home/natty/wor...')\n#38 {main}"},"jsonrpc":"2.0"}
文档有错误,库的开发者建议我更改:
curl 'http://127.0.0.1:8000/api/test1' --data-binary
'{"jsonrpc":"2.0","method":"pharmacies@ping","params":["innings":
"out"],"id" : 1}'
至
curl 'http://127.0.0.1:8000/api/test1' --data-binary
'{"jsonrpc":"2.0","method":"pharmacies@ping","params":{"innings":
"out"},"id" : 1}'
因此将“params”中的 [] 更改为 {} 使其有效!
我加 https://github.com/sajya/server
对我的 laravel 8 进行 json rpc。
我只是按照此处提供的说明进行测试 https://sajya.github.io/docs/
所有基本示例都适用,但如果我将参数添加到 curl 字符串,则会引发错误,我无法理解:
我的代码:
<?php
declare(strict_types=1);
namespace App\Http\Procedures;
use Sajya\Server\Procedure;
use Illuminate\Http\Request;
class PharmaciesProcedure extends Procedure
{
/**
* The name of the procedure that will be
* displayed and taken into account in the search
*
* @var string
*/
public static string $name = 'pharmacies';
public function ping(Request $request)
{
return $request->input('innings');
}
}
我的路线api文件:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Procedures\PharmaciesProcedure;
Route::rpc('/test1', [PharmaciesProcedure::class])->name('rpc.endpoint');
此请求有效:
curl 'http://127.0.0.1:8000/api/test1' --data-binary '{"jsonrpc":"2.0","method":"pharmacies@ping","params":[],"id" : 1}'
这不是:
curl 'http://127.0.0.1:8000/api/test1' --data-binary '{"jsonrpc":"2.0","method":"pharmacies@ping","params":["innings": "out"],"id" : 1}'
错误:
{"id":null,"error":{"code":-32700,"message":"Parse error","data":null,"file":"/home/natty/workspace/farmacie/vendor/sajya/server/src/Http/Parser.php","line":133,"trace":"#0 /home/natty/workspace/farmacie/vendor/sajya/server/src/Http/Parser.php(104): Sajya\Server\Http\Parser->checkValidation()\n#1 /home/natty/workspace/farmacie/vendor/sajya/server/src/Guide.php(49): Sajya\Server\Http\Parser->makeRequests()\n#2 /home/natty/workspace/farmacie/vendor/sajya/server/src/JsonRpcController.php(27): Sajya\Server\Guide->handle()\n#3 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(48): Sajya\Server\JsonRpcController->__invoke()\n#4 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(255): Illuminate\Routing\ControllerDispatcher->dispatch()\n#5 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): Illuminate\Routing\Route->runController()\n#6 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\Routing\Route->run()\n#7 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}()\n#8 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#9 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle()\n#10 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#11 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(103): Illuminate\Routing\Middleware\ThrottleRequests->handleRequest()\n#12 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(55): Illuminate\Routing\Middleware\ThrottleRequests->handleRequestUsingNamedLimiter()\n#13 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\ThrottleRequests->handle()\n#14 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#15 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\Pipeline\Pipeline->then()\n#16 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668): Illuminate\Routing\Router->runRouteWithinStack()\n#17 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634): Illuminate\Routing\Router->runRoute()\n#18 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\Routing\Router->dispatchToRoute()\n#19 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\Routing\Router->dispatch()\n#20 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}()\n#21 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#22 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()\n#23 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#24 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()\n#25 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#26 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle()\n#27 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#28 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle()\n#29 /home/natty/workspace/farmacie/vendor/fruitcake/laravel-cors/src/HandleCors.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#30 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle()\n#31 /home/natty/workspace/farmacie/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#32 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle()\n#33 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()\n#34 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then()\n#35 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()\n#36 /home/natty/workspace/farmacie/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle()\n#37 /home/natty/workspace/farmacie/server.php(21): require_once('/home/natty/wor...')\n#38 {main}"},"jsonrpc":"2.0"}
文档有错误,库的开发者建议我更改:
curl 'http://127.0.0.1:8000/api/test1' --data-binary '{"jsonrpc":"2.0","method":"pharmacies@ping","params":["innings": "out"],"id" : 1}'
至
curl 'http://127.0.0.1:8000/api/test1' --data-binary '{"jsonrpc":"2.0","method":"pharmacies@ping","params":{"innings": "out"},"id" : 1}'
因此将“params”中的 [] 更改为 {} 使其有效!