0

I want get data in JSON format from odoo controllery.py

Example:

import openerp.http as http
from openerp.http import request

class MyController(http.Controller):

    @http.route('/test_html', type="http", auth="public")
    def some_html(self):
        return "<h1>Test</h1>"

    #Work fine when open http://localhost:8069/test.html

    @http.route('/test_json', type="json", website=True, auth="public")
    def some_json(self):
        return [{"name": "Odoo", 'website': 'www.123.com'}]

How get data in json format, I want data from json read in other app with ajax.

Is it possible view json after open url http://localhost:8069/test_json ???

Naglis
  • 2,583
  • 1
  • 19
  • 24
Anastasia_
  • 289
  • 1
  • 3
  • 17

2 Answers2

2

The important part is to define the contentType of your request properly.

import json

@http.route('/test_json', type="json", auth="public")
def some_json(self):
    return json.dumps({"name": "Odoo", 'website': 'www.123.com'})

In your client using javascript you can request the json like this.

$.ajax({ 
        type: "POST", 
        url: "/test_json", 
        async: false, 
        data: JSON.stringify({}), 
        contentType: "application/json", 
        complete: function (data) { 
            console.log(data);  
        } 
});

Or using requests in python

import requests,json

res = requests.post("http://localhost:8069/test_json",data=json.dumps({}),headers={"Content-Type":"application/json"})

To access the response body

body = res.text

As to whether you can simply open a browser and view the json. No, not by default.

Here is what I get

Bad Request

<function some_json at 0x7f48386ceb90>, /test_json: Function declared as capable of handling request of type 'json' but called with a request of type 'http'

You could probably do something pretty fancy with a controller if you really wanted to be able to view it in a browser as well as make json requests. I would post a second question though.

Phillip Stack
  • 3,308
  • 1
  • 15
  • 24
  • @Philip Stack Tnx for example, Now any simple example how replace return json.dumps({"name": "Odoo", 'website': 'www.123.com'}) with record from my databse table (res.users). – Anastasia_ Feb 18 '17 at 13:06
  • To access records you use `http.request.env.user' which would be the current user. To specify a different user `http.request.env['res.users'].sudo().browse([user_id])` from there you can build it into your response variable. – Phillip Stack Feb 18 '17 at 15:19
  • Hello Phillip, Here is my example https://postimg.org/image/sa4q1miur/ after call get resonse 200, where is problem? – Anastasia_ Feb 20 '17 at 08:41
  • How view data from controller? – Anastasia_ Feb 20 '17 at 11:51
  • I will update my answer. It really depends if you are requesting the data from the browser (javascript), or a different server side language. – Phillip Stack Feb 20 '17 at 13:47
  • I think your question has been addressed, if you could upvote my answer and mark it as correct I would appreciate it. – Phillip Stack Feb 20 '17 at 13:50
0

Your controller endpoint looks ok and should function correctly, so I guess your main question is how to test it.

Once you declare that the endpoint type is json, Odoo will check that the request content type header is in fact JSON, so in order to test it your requests will need to have Content-Type: application/json header set. This is a bit difficult using a regular browser, unless you edit the request headers before seinding or call your JSON endpoint from JavaScript via Ajax.

Alternatively, you can test your API from command line using a tool like curl:

curl 'http://localhost:8069/test_json' -H 'Content-Type: application/json' --data "{}"

--data "{}" here indicates an empty JSON structure which will be passed to your endpoint as request parameters.

Please note that you might also have to pass an additional header containing your session_id cookie if you are using more than one Odoo database.

Naglis
  • 2,583
  • 1
  • 19
  • 24