python 2.7 - How to get JSON data in an Odoo controller using type='json'? -


a few days ago did similar question here: how json data in odoo controller?

but now, need create controller receives json data. so, doing request python console, way:

import requests import json  url = 'http://localhost:8069/odoo/test' headers = {'content-type': 'application/json'} data = {     'name': 'jane',     'email': 'jane.doe@gmail.com', } data_json = json.dumps(data) r = requests.post(url=url, data=data_json, headers=headers) 

i have created controller listens http://localhost:8069/odoo/test, way:

import openerp.http http openerp.http import response import logging _logger = logging.getlogger(__name__)   class webformcontroller(http.controller):      @http.route('/odoo/test', type='json',                 auth='public', methods=['post'], website=true)     def index(self, **args):         _logger.info('connection successful')         _logger.info(args)         name = args.get('name', false)         email = args.get('email', false)         _logger.info(name)         _logger.info(email)         if not name:             response.status = '400 bad request'         return '{"response": "ok"}' 

the problem receiving empty json in controller. can read connection succesful in log, no error, when show args, {}, , due that, false when writing name , email.

if pass data python dictionary or string, following error:

invalid json data: 'name=jane&email=jane.doe%40gmail.com' or invalid json data: "{'name': 'jane', 'email': 'jane.doe@gmail.com'}" respectively.

if modify type='json' , write type='http' instead, following error:

function declared capable of handling request of type 'http' called request of type 'json'.

i have read may solved if request sent using parameter json instead of data, way:

r = requests.post(url=url, json=data_json, headers=headers) 

unfortunately, server going make request has old operating system cannot update python-requests package, cannot use json parameter since did not exist @ version installed in server.

please, can me? need json data in controller, not string neither python dictionaries.

you have forgotten put data inside params keywords:

use correct syntax :

data = {"params": dict(key="value")}

data = {     "params": {         "name":"prakashsharma",         "email":"prakashsharmacs24@gmail.com",         "phone":"+917859884833"     } } 

please don't forget use json.dumps(data) , 'content-type': 'application/json' while requesting resource in json format.

i damn sure issue solved after using 1 friend... cheers :)!!


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -