Making a JSON-RPC HTTP call using C#
Asked Answered
B

2

8

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.

Bosomed answered 13/3, 2014 at 22:29 Comment(1)
Have a look at the HttpWebRequest class on MSDN.Onomatopoeia
S
5

Use this:

using (var webClient = new WebClient())
{
    var response = webClient.UploadString("http://user:pass@localhost:8080/jsonrpc", "POST", json);
}
Stretchy answered 13/3, 2014 at 22:45 Comment(2)
Are you sure? This is a POST whereas he wants to do a GETReadjust
As per their documentation, you can POST (by uploading JSON in the request BODY) or GET using the request param.Stretchy
B
4

@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);
}
Bourassa answered 26/10, 2016 at 15:1 Comment(1)
Correct, and for a simple reason :-) Ganesh assumed that the request is made using Base64, but the RPC endpoint in Kodi (At least from v16 and up [Jarvis] ) is not base64 secured, well at least not in the traditional sense. Doing it using the net credentials does something extra that works, where as the inline doesn't work unless your Kodi ver is less than 16.Evetta

© 2022 - 2024 — McMap. All rights reserved.