Search code examples
resttypescriptwakanda

Wakanda 2 - REST API for calling an RPC


I would like to use REST calls to call my serverside RPC module functions.

I actually have something close in-place already but it gives me a 500 internal server error and I don't know how to troubleshoot. Below is what I have. RPC is enabled for the module and in my project settings. CORS is allowed.

rpc(moduleName: string, methodName: string, params: any[]): Promise<any> {
        let data = {
            jsonrpc: '2.0',
            //id: 167972631107,
            module: moduleName,
            method: methodName,
            params: params
        };

        let headers = new Headers({
            'Content-Type': 'application/json'
        });

        return this.http.post(`http://127.0.0.1:8081/rpc/`, JSON.stringify(data), headers).toPromise();
    }

Solution

  • First, check whether the module has permissions to be executed from client side:

    <?xml version="1.0" encoding="UTF-8"?>
        <permissions>
            <allow type="module" resource="myRPC" action="executeFromClient"/>
        </permissions>
    

    Second, 500 error might indicate a unhandled error in server-side execution due to incorrect parameter sent by the client, or simply bad code in the module. You can use the browser network inspector to validate the data sent by POST request. Or put a "debugger" trace statement in RPC code to set a breakpoint. The execution will stop at the breakpoint if the request is received by the server, you can then inspect the runtime from there.