Skip to content
Advertisement

json rpc 2.0 in laravel api errors on add request

I add https://github.com/sajya/server

for making json rpc over my laravel 8.

I just follow the instructions provided here to test it https://sajya.github.io/docs/

All works for the basic example, But if I add params to the curl string an error is raised an error that i cannot understand:

my code:

<?php

declare(strict_types=1);

namespace AppHttpProcedures;
use SajyaServerProcedure;

use IlluminateHttpRequest;

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');
    }
}

my route api file:

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpProceduresPharmaciesProcedure;


Route::rpc('/test1', [PharmaciesProcedure::class])->name('rpc.endpoint');

this request works:

curl ‘http://127.0.0.1:8000/api/test1’ –data-binary ‘{“jsonrpc”:”2.0″,”method”:”pharmacies@ping”,”params”:[],”id” : 1}’

This not:

curl ‘http://127.0.0.1:8000/api/test1’ –data-binary ‘{“jsonrpc”:”2.0″,”method”:”pharmacies@ping”,”params”:[“innings”: “out”],”id” : 1}’

the error:

{“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): SajyaServerHttpParser->checkValidation()n#1 /home/natty/workspace/farmacie/vendor/sajya/server/src/Guide.php(49): SajyaServerHttpParser->makeRequests()n#2 /home/natty/workspace/farmacie/vendor/sajya/server/src/JsonRpcController.php(27): SajyaServerGuide->handle()n#3 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(48): SajyaServerJsonRpcController->__invoke()n#4 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(255): IlluminateRoutingControllerDispatcher->dispatch()n#5 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): IlluminateRoutingRoute->runController()n#6 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): IlluminateRoutingRoute->run()n#7 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): IlluminateRoutingRouter->IlluminateRouting{closure}()n#8 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#9 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateRoutingMiddlewareSubstituteBindings->handle()n#10 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#11 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(103): IlluminateRoutingMiddlewareThrottleRequests->handleRequest()n#12 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(55): IlluminateRoutingMiddlewareThrottleRequests->handleRequestUsingNamedLimiter()n#13 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateRoutingMiddlewareThrottleRequests->handle()n#14 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#15 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): IlluminatePipelinePipeline->then()n#16 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668): IlluminateRoutingRouter->runRouteWithinStack()n#17 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634): IlluminateRoutingRouter->runRoute()n#18 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): IlluminateRoutingRouter->dispatchToRoute()n#19 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): IlluminateRoutingRouter->dispatch()n#20 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}()n#21 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#22 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareTransformsRequest->handle()n#23 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#24 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareTransformsRequest->handle()n#25 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#26 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareValidatePostSize->handle()n#27 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#28 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewarePreventRequestsDuringMaintenance->handle()n#29 /home/natty/workspace/farmacie/vendor/fruitcake/laravel-cors/src/HandleCors.php(57): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#30 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): FruitcakeCorsHandleCors->handle()n#31 /home/natty/workspace/farmacie/vendor/fideloper/proxy/src/TrustProxies.php(57): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#32 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): FideloperProxyTrustProxies->handle()n#33 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}()n#34 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): IlluminatePipelinePipeline->then()n#35 /home/natty/workspace/farmacie/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): IlluminateFoundationHttpKernel->sendRequestThroughRouter()n#36 /home/natty/workspace/farmacie/public/index.php(52): IlluminateFoundationHttpKernel->handle()n#37 /home/natty/workspace/farmacie/server.php(21): require_once(‘/home/natty/wor…’)n#38 {main}”},”jsonrpc”:”2.0″}

Advertisement

Answer

there was an error on documentation, the developer of the libs suggest me to change:

curl ‘http://127.0.0.1:8000/api/test1’ –data-binary ‘{“jsonrpc”:”2.0″,”method”:”pharmacies@ping”,”params”:[“innings”: “out”],”id” : 1}’

to

curl ‘http://127.0.0.1:8000/api/test1’ –data-binary ‘{“jsonrpc”:”2.0″,”method”:”pharmacies@ping”,”params”:{“innings”: “out”},”id” : 1}’

so changing [] to {} in “params” make it works!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement