# Client side JsonRPC calls

**URL:** https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222
**Category:** Client
**Tags:** 2022-06, rpc
**Created:** [January 18, 2023, 1:45pm UTC](https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222 "2023-01-18T13:45:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stefan-sparse-array](https://discourse.geta12.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@stefan-sparse-array](https://discourse.geta12.com/u/stefan-sparse-array)
#### Post date: [January 18, 2023, 1:45pm UTC](https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222/1 "2023-01-18T13:45:52Z")

</div>

I’m currently upgrading from A12 2021.06 to 2022.06. Within this upgrade we are forced to use the new JsonRPC operations.  
As we are using a custom data loader we need to execute e.g. the `LIST_DOCUMENTS` RPC calls on our own.

Is there some public utility to create such RPC payload and dispatch it afterwards?

I found the `RequestBuilder` builder and the `dispatchJsonRpc2Requests` function that exactly provides such a functionallity but unfortunately are for internal use only.

Cheers  
Stefan

---

<div class="post-metadata">

### Author: ![markus-agile-fog](https://discourse.geta12.com/user_avatar/discourse.geta12.com/markus-agile-fog/32/1877_2.png) [@markus-agile-fog](https://discourse.geta12.com/u/markus-agile-fog)
#### Post date: [January 18, 2023, 1:54pm UTC](https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222/2 "2023-01-18T13:54:27Z")

</div>

Hi @stefan-sparse-array,

at least the request builder should be public. For doing the request you can just call the fetch method of the default connector. Here is a simplified example:

```typescript
import { JsonRpc2Request} from "@com.mgmtp.a12.dataservices/dataservices-access/lib/json-rpc";
import { ConnectorLocator } from "@com.mgmtp.a12.utils/utils-connector/lib/main";

const request = JsonRpc2Request.build(rpcRequests);
const documentsResponse = (await ConnectorLocator.getInstance().getServerConnector().fetchData(request)) as Response;

```

---

<div class="post-metadata">

### Author: ![stefan-cold-haze](https://discourse.geta12.com/user_avatar/discourse.geta12.com/stefan-cold-haze/32/2003_2.png) [@stefan-cold-haze](https://discourse.geta12.com/u/stefan-cold-haze)
#### Post date: [January 18, 2023, 2:09pm UTC](https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222/3 "2023-01-18T14:09:35Z")

</div>

Hi @stefan-sparse-array,

for me it does not seem difficult. With the right typing the request is easily build. Here a example of your use case:

```auto
import {
	DocumentJsonRpc2Request,
	JsonRpc2Request,
	ListDocumentsJsonRpc2Response
} from "@com.mgmtp.a12.dataservices/dataservices-access";
import {
	ConnectorLocator,
	RestRequestPayload,
	RestServerConnector
} from "@com.mgmtp.a12.utils/utils-connector/lib/main";

const request: DocumentJsonRpc2Request.ListDocumentsJsonRpc2Request = {
	id: "any",
	jsonrpc: "2.0",
	method: "LIST_DOCUMENTS",
	params: {
		documentModelName: "xxx",
		filter: { filters: [], fulltext: "", lang: "en" },
		page: {},
		sort: []
	}
};

const serverConnector = ConnectorLocator.getInstance().getServerConnector();
if (!(serverConnector instanceof RestServerConnector)) {
	throw new Error(`"ServerConnector" needs to be a "RestServerConnector".`);
}

try {
	const painDocumentList = serverConnector
		.fetchData(JsonRpc2Request.build([request]))
		.then(response => response.json())
		.then(data =>
			!Array.isArray(data) || !ListDocumentsJsonRpc2Response.isInstance(data[0])
				? Promise.reject("Data is not suitable!")
				: data[0]
		);
} catch (e: unknown) {
	console.error("An error occurred while fetching data.", e);
}

```

---

<div class="post-metadata">

### Author: ![stefan-sparse-array](https://discourse.geta12.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@stefan-sparse-array](https://discourse.geta12.com/u/stefan-sparse-array)
#### Post date: [January 18, 2023, 4:41pm UTC](https://discourse.geta12.com/t/client-side-jsonrpc-calls/1222/4 "2023-01-18T16:41:31Z")

</div>

@markus-agile-fog , @stefan-cold-haze thanks a lot for the examples but it feels like this is something each and every project communicating with the platform server needs to implement anyway, thus maybe an official service would be helpful and would reduce implementation effort even more.
