Search code examples
jsonapicurljson-rpc

How to use JSON-RPC POST request?


I never used JSON-RPC before and a client of mine gave me some API credentials to fetch the info to post on the website. So, I never heard about JSON_RPC before and the only thing I have is this:

curl -s -u MyID:MyPassword -X POST -k --header "Content-type: 
application/json" --data-binary '{"jsonrpc": "2.0","method": "ListOfContent","params": 
{},"id":7123}'

So, given that, how to use it, with what to use it, how to retrieve the data?


Solution

  • An example from here

    async function getBalanceUsingJSONRPC(address: string): Promise<number> {
        const url = clusterApiUrl('devnet')
        console.log(url);
        return fetch(url, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "getBalance",
                "params": [
                    address
                ]
            })
        }).then(response => response.json())
        .then(json => {
            if (json.error) {
                throw json.error
            }
    
            return json['result']['value'] as number;
        })
        .catch(error => {
            throw error
        })
    }
    

    you can read more about JSON RPC specifications