Search code examples
getarduinohttprequestjson-rpcxbmc

check XBMC with Arduino


A week ago I started a new project with my Arduino MEGA 2560 with Ethernet Shield. I'm able to send a WOL to my server but now I want to PING my XBMC server with the JSON-RPC command. With my Chrome browser I'm able to check the server with follwoing HTTP GET request:

http://192.168.5.34/jsonrpc?request={"jsonrpc": "2.0", "id": 1, "method": "JSONRPC.Ping"}

And as a result I get a clean PONG back from the XBMC server:

{"id":1,"jsonrpc":"2.0","result":"pong"}

Now I tried to do this with my Arduino with the following code that is embedded in a function:

EthernetClient client;
IPAddress server(192,168,5,34);
if (client.connect(server, 80))
{
  Serial.println("Connecting to Client...");
  client.print("GET /jsonrpc?request={%22jsonrpc%22:%20%222.0%22,%20%22id%22:%201,%20%22method%22:%20%22JSONRPC.Ping%22} HTTP/1.1\r\n");
  client.print("Host: 192.168.5.34\r\n");
  client.print("User-Agent: Mozilla/5.0\r\n");
  client.print("Connection: close\r\n\r\n");
}
else
    Serial.println("Client Connection Failed!");

With my Arduino a got following return:

HTTP/1.1 401 Unauthorized
Content-Length: 0
Connection: close
WWW-Authenticate: Basic realm=XBMC
Date: Fri, 06 Jun 2014 21:33:24 GMT

I can make a GET request to Google with my Arduino so it's really XBMC that for some reason does not accept my GET request. Anyone got an idea why? I used Wireshark to sniff the network data on the server and the request and return are present in Wireshark.

Is there maybe a way to login on the XBMC server by GET or POST?

Thanks for the help guys! :)


Solution

  • WWW-Authenticate: Basic realm=XBMC
    

    This means that your server requested your user agent (Arduino in this case) to authenticate using basic access authentication.
    Adding this line should do the trick:

     client.print("Authorization: Basic xxxxxxxxxxxxxxxxxxxx\r\n");
    

    where xxxxxxxxxxxxxxxxxxxx is Base64 encoded (more precisely RFC2045-MIME variant of Base64 is used, except not limited to 76 char/line) username and password. In order to obtain it, you can use Wireshark to capture this request header while sending request from your browser, or generate it:

    1. Check in your XBMC settings (Settings > Services > Webserver) for the username/password. It is by default xbmc:xbmc.
    2. Go to http://www.motobit.com/util/base64-decoder-encoder.asp and in the first textbox put your username and password using format: username:password, e.g. xbmc:xbmc. Select encode option, change Maximum characters per line: to some big value, e.g. 500 and press Convert the source data.
    3. Copy the Base64 representation (for xbmc:xbmc it will be eGJtYzp4Ym1j and use it in your header, e.g.:

      client.print("Authorization: Basic eGJtYzp4Ym1j\r\n");

    You can also use https://github.com/adamvr/arduino-base64 library for the base64 encoding.

    The reason why it works in your browser is that it may have already been authenticated.