Calling JSON-RPC with HTTP GET request?

232 views
Skip to first unread message

tac

unread,
Sep 21, 2011, 1:58:52 PM9/21/11
to Jayrock
Does Jayrock suport JSON-RPC calls using GET instead of POST like
someurl?jsonrpc=<JReq UrlEnc>

I know the standard defines POST for JSON-RPC 2.0 but in some cases
using GET would be faster and simpler.

Atif Aziz

unread,
Sep 21, 2011, 5:30:34 PM9/21/11
to jay...@googlegroups.com
Yes it does. More specifically (and out-of-the-box), Jayrock supports HTTP GET call encoding as laid out in the JSON-RPC 1.1 Working Draft document:

Bear in mind, 1.1 is not an official JSON-RPC version as the specification remained a working draft and was superseded by 2.0.

In Jayrock, you must mark a method as being idempotent for you to invoke it using HTTP GET, as shown here:

[ JsonRpcMethod("add", Idempotent = true)]
[ JsonRpcHelp("Return the sum of two integers.") ]
public int Add(int a, int b) {
  return a + b;
}

The above was taken from the demo service supplied with Jayrock:

You would invoke such a method as follows (live version):

- Atif


--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To post to this group, send email to jay...@googlegroups.com.
To unsubscribe from this group, send email to jayrock+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jayrock?hl=en.


tac

unread,
Sep 22, 2011, 1:13:46 PM9/22/11
to Jayrock
But what I am looking is to call GET with parameters that can be
objects not just plain values, like for a request like:

{ "jsonrpc": "2.0", "id": 1, "method": "mxx", "params": [1,{"name":
"XX", "age": 33, "address": { "street": "zz", "number": 11}}]}}

do a call like

http://xx/yy?jsonrpc=%7B%27params%27%3A+%5B1%2C+%7B%27age%27%3A+33%2C+%27name%27%3A+%27XX%27%2C+%27address%27%3A+%7B%27street%27%3A+%27zz%27%2C+%27number%27%3A+11%7D%7D%5D%2C+%27jsonrpc%27%3A+%272.0%27%2C+%27id%27%3A+1%2C+%27method%27%3A+%27mxx%27%7D

Atif Aziz

unread,
Sep 23, 2011, 2:21:14 PM9/23/11
to jay...@googlegroups.com
But what I am looking  is to call GET with parameters that can be
objects not just plain values, like for a request like:

While I wouldn't exactly recommend doing this for various reasons, it's technically possible and a breeze to teach Jayrock to do just that.

If you inherit from JsonRpcHandler then there's a method you can override called GetFeatureByName and use it to hook and decorate your service with additional functionality. In fact, all JSON-RPC over HTTP infrastructure in Jayrock is implemented this way, including the help and test pages. The feature gets to decide how the service interacts with the HTTP request. Let's call your feature "tunnel" and when GetFeatureByName gets called with this name, you'll return an object that implements the feature:

protected override object GetFeatureByName(string name) {
  return ("tunnel".Equals(name, StringComparison.OrdinalIgnoreCase))
       ? new JsonRpcGetTunnelProtocol(this);
       : base.GetFeatureByName(name);
}

The above can be just thrown in your service class that inherits from JsonRpcHandler (it can also be applied via configuration). I have placed the implementation of JsonRpcGetTunnelProtocol over at (and also attached a copy):


It's fairly small, straightforward and adapted from the JsonRpcGetProtocol class supplied with Jayrock. It reads the JSON-RPC request from a query string parameter called "req" and uses a JsonRpcDispatcher instance to invoke the call on the service (supplied during construction) and then write out the JSON-RPC response object.

When an HTTP request comes in, JsonRpcHandler looks for the feature name as the value of the an unnamed query string parameter. So to invoke your service using the new tunneling approach, you would invoke with a URL like this:


The details can all be changed but this gives you an idea that it is certainly possible with little effort.

- Atif


--
JsonRpcGetTunnelProtocol.cs
Reply all
Reply to author
Forward
0 new messages