Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

What is the difference between jsonrequest and httprequest?

By
Gopakumar N G
on 11/13/13, 2:29 AM 1,596 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 ?

1

Gopakumar N G

--Gopakumar N G--
1198
| 5 3 7
Cochin, India
--Gopakumar N G--

Senior Software Engineer at BroadTech IT Solutions Pvt. Ltd.

Gopakumar N G
On 11/26/13, 4:50 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 11/13/13, 2:29 AM
Seen: 1596 times
Last updated: 3/16/15, 8:10 AM