Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between jsonrequest and httprequest?

Tags:

json

http

openerp

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 ?

like image 674
Gopakumar N G Avatar asked Nov 13 '13 06:11

Gopakumar N G


People also ask

What is request and response in JSON?

json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

What is JSON post?

JSON (JavaScript Object Notation) is the most widely used data format for data interchange on the web.

Is HTTP a response JSON?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.


1 Answers

I didn't look at OpenERP v7 but OpenERP v6.1 uses both - HttpRequest and JsonRequest. I suppose it's the same for OpenERP v7...

Both of them are about 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 look here for 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.

EDIT - some more words about the decorators @openerpweb.jsonrequest and @openerpweb.httprequest

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? And 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.

like image 94
Andrei Boyanov Avatar answered Oct 18 '22 03:10

Andrei Boyanov