Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JSON-RPC HTTP call using C#

Tags:

c#

.net

json-rpc

I would like to know how to make the following HTTP call using C#.

http://user:pass@localhost:8080/jsonrpc?request={"jsonrpc":"2.0","method":"VideoLibrary.Scan"}

When I paste this url directly into my browser (and replace the user credentials & server location), it works as expected (my XBMC video library is updated). It relates specifically to the HTTP methods on this page:

http://wiki.xbmc.org/index.php?title=HOW-TO:Remotely_update_library

I'd like to know how to make this same successful call over HTTP using C# though.

like image 287
marcusstarnes Avatar asked Mar 13 '14 22:03

marcusstarnes


People also ask

What is a JSON-RPC call?

JSON-RPC is simply a remote procedure call protocol that is used on Ethereum to define different data structures. It also defines the rules on how data structures are processed in the network. Because it is transport-agnostic, you can use it to interact with an ETH node over sockets or HTTP.

Does RPC support JSON?

Based upon the JSON-RPC version used, the remote system will send various data outputs back to the source of request. All the JSON-RPC powered web transfers are unified and serialized with the help of JSON only.

Is JSON-RPC an API?

Consensus client APIs This page deals mainly with the JSON-RPC API used by Ethereum execution clients. However, consensus clients also have an RPC API that allows users to query information about the node, request Beacon blocks, Beacon state, and other consensus-related information directly from a node.

Does postman support RPC?

Pascal Charbonneau. JSON RPC endpoints are simple POST so you should be able to do that with Postman. Just make sure you set your content-type to be 'application/json' and set your POST body accordingly. I personally prefer the Advanced Rest Client for its simplicity and beautiful interface.


2 Answers

Use this:

using (var webClient = new WebClient())
{
    var response = webClient.UploadString("http://user:pass@localhost:8080/jsonrpc", "POST", json);
}
like image 125
Ganesh Avatar answered Sep 20 '22 06:09

Ganesh


@Ganesh

I kept getting HTTP 401 Unauthorized messages until I added a reference to the network credentials (using http://username:password@server:port just didn't work for me)

using (var webClient = new WebClient())
{
  // Required to prevent HTTP 401: Unauthorized messages
  webClient.Credentials = new NetworkCredential(username, password);
  // API Doc: http://kodi.wiki/view/JSON-RPC_API/v6
  var json = "{\"jsonrpc\":\"2.0\",\"method\":\"GUI.ShowNotification\",\"params\":{\"title\":\"This is the title of the message\",\"message\":\"This is the body of the message\"},\"id\":1}";
  response = webClient.UploadString($"http://{server}:{port}/jsonrpc", "POST", json);
}
like image 33
Neal Bailey Avatar answered Sep 20 '22 06:09

Neal Bailey