如何使用type ='json'在Odoo控制器中获取JSON数据? [英] How to get JSON data in an Odoo controller using type='json'?
问题描述
几天前,我在这里做了类似的问题:如何在Odoo控制器中获取JSON数据?
但是现在,我需要创建一个仅接收JSON数据的控制器.因此,我是通过以下方式从Python控制台执行请求的:
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)
我创建了一个监听 http://localhost:8069/odoo/test 的控制器,这样:
import openerp.http as http
from 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"}'
问题是我在控制器中收到一个空的JSON.我可以在日志中读取成功连接,没有错误,但是当我显示args
时,我得到了{}
,显然由于这个原因,写name
和<时显然是False
c4>.
如果我将数据作为Python字典或字符串传递,则会出现以下错误:
Invalid JSON data: 'name=Jane&email=jane.doe%40gmail.com'
或
Invalid JSON data: "{'name': 'Jane', 'email': 'jane.doe@gmail.com'}"
.
如果我修改type='json'
而改写type='http'
,则会出现以下错误:
Function declared as capable of handling request of type 'http' but called with a request of type 'json'
.
我已经读到,如果使用参数json
而不是data
发送请求,可以通过以下方式解决此问题:
r = requests.post(url=url, json=data_json, headers=headers)
不幸的是,要发出请求的服务器具有一个旧的操作系统,该操作系统无法更新python-requests
软件包,因此我无法使用json
参数,因为该服务器上安装的版本中不存在该参数. /p>
请,有人可以帮助我吗?我需要在控制器中获取JSON数据,而不是Python字典中的字符串.
您刚刚忘记将数据放入 params 关键字内:
使用正确的语法:
data = {" params ":dict(key ="value")}
data = {
"params": {
"name":"prakashsharma",
"email":"prakashsharmacs24@gmail.com",
"phone":"+917859884833"
}
}
在请求json格式的资源时,请不要忘记使用json.dumps(data)
和'Content-Type': 'application/json'
.
我确定我的朋友使用这个后,您的问题会得到解决...干杯:)!!
A few days ago I did a similar question here: How to get JSON data in an Odoo controller?
But now, I need to create a controller which receives only JSON data. So, I am doing the request from a Python console, this 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 a controller which listens to http://localhost:8069/odoo/test, this way:
import openerp.http as http
from 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 is that I am receiving an empty JSON in the controller. I can read CONNECTION SUCCESFUL in the log, with no error, but when I show args
, I get {}
, and obviously due to that, False
when writing name
and email
.
If I pass the data as a Python dictionary or as a string, I get the 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 I modify type='json'
and I write type='http'
instead, I get the following error:
Function declared as capable of handling request of type 'http' but called with a request of type 'json'
.
I have read that may be this could be solved if the request is sent using the parameter json
instead of data
, this way:
r = requests.post(url=url, json=data_json, headers=headers)
Unfortunately, the server which is going to make the request has an old operating system which cannot update the python-requests
package, so I cannot use json
parameter since it did not exist at the version installed in that server.
Please, can anyone help me? I need get JSON data in the controller, not a string neither Python dictionaries.
You have just forgotten to put your data inside the params keywords:
Use this correct syntax :
data = {"params": dict(key="value")}
data = {
"params": {
"name":"prakashsharma",
"email":"prakashsharmacs24@gmail.com",
"phone":"+917859884833"
}
}
Please don't forget to use json.dumps(data)
and 'Content-Type': 'application/json'
while requesting a resource in json format.
I am damn sure your issue will be solved after using this one my friend... cheers :)!!
这篇关于如何使用type ='json'在Odoo控制器中获取JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!