shogowada / json-rpc-2.0

Let your client and server talk over function calls under JSON-RPC 2.0 spec. Strongly typed. No external dependencies.

Home Page:https://www.npmjs.com/package/json-rpc-2.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to support jsonrpc2.0 batch

sighWang opened this issue · comments

I have call jsonrpcserver with valid Batch, But there is error: { code: -32600, message: 'Invalid Request' } return to me

Thanks for the catch. It doesn't support bulk requests as of now but let me add it!

I think it actually requires a breaking change, so let me think about it for a while.

In the meantime, you can workaround it by doing something like this:

const jsonRPCServer = new JSONRPCServer();

app.post("/json-rpc", async (req, res) => {
  const request = req.body;
  if (Array.isArray(request)) {
    const responses = await Promise.all(request.map(request => jsonRPCServer.receive(request)));
    res.json(responses);
  } else {
    const response = await jsonRPCServer.receive(request);
    res.json(response);
  }
});

@sighWang It is now released under v1.0.0: #26