This question has been flagged
1 Reply
8604 Views

I was checking the files in the controllers of web module in both OpenERP-7.0 and OpenERP-6.1. Then I found that 6.1 uses jsonrequest (@openerpweb.jsonrequest) 7.0 uses httprequest (@openerpweb.httprequest). What is the difference between the two ?

Avatar
Discard
Author Best Answer

Difference between jsonrequest and httprequest

Both jsonrequest and httprequest are used for communication between client and server. HttpRequest communicates trough the well known GET and POST methods. That means the following:

  • The client send a request encoded in the url (GET method) or in the http body (POST method)
  • The server returns an object corresponding to the request. Could be an html page, PNG image, CSS file, JavaScript, XML encoded data or whatever.

JsonRequest is an implementation of another protocol for client/server communication - JSON-RPC 2.0. You may want lo took here form more information. It's a remote procedure call (RPC) protocol which means that it allows the client to initiate the execution of some method on the server passing some arguments to this method. In response the client gets some data as a result of the method invocation.

Some methods are decorated with the @openerpweb.jsonrequest decorator, other methods - with the @openerpweb.httprequest. This means nothing else but that the first group of methods will be available for execution trough the JSON RPC protocol and the second group will be accessible trough the pure HTTP protocol.

Now, what is the difference? I do we need both jsonrequest and httprequest? Let simplify it like this: JSON is more suitable for executing methods on the server and obtain results. HTTP is simpler and easier to use when all we what is to access some resource on the server.

Let's 'decorate' this with some examples for clarity. Take a look at the following method of the web.controllers.main.Export class:

@openerpweb.jsonrequest
def formats(self, req):
    """ Returns all valid export formats

    :returns: for each export format, a pair of identifier and printable name
    :rtype: [(str, str)]
    """
    ...

This method accepts some arguments and returns a list (Python list object) containing all known export formats. It will be called in a programmatic way in some python code on the client side.

On the other side are the 'http' methods - like the method css() of the web.controllers.main.Web class:

@openerpweb.httprequest
def css(self, req, mods=None):
    ....

All this method does is to return a CSS file to the client. It's a simple action like accessing an image, a HTML web page or whatever other resource on the server. The resource we are returning here is nothing complicated as a Python list as in the previous example. We don't need a special format to encode it additionally. So we don't need additional data encoding format as JSON and remote procedure call protocol as JSON RPC.

Courtesy: Andrei Boyanov

Avatar
Discard