如何在Odoo控制器中获取JSON数据? [英] How to get JSON data in an Odoo controller?
问题描述
我试图向Odoo控制器发送一些JSON数据,但是当我发送请求时,我总是得到404作为响应。
I am trying to send some JSON data to an Odoo controller, but when I send the request, I always get 404 as response.
这是代码我的控制器:
import openerp.http as http
import logging
_logger = logging.getLogger(__name__)
class Controller(http.Controller):
@http.route('/test/result', type='json', auth='public')
def index(self, **args):
_logger.info('The controller is called.')
return '{"response": "OK"}'
现在,我键入网址( http:// localhost:8069 / test / result ),以检查是否可用,并获得函数索引0x7f04a28> ; / test / result:函数声明为能够处理类型'json'的请求,但是使用类型'http'
的请求进行调用。这样我知道控制器正在侦听该URL并且正在等待JSON数据。
Now, I type the URL (http://localhost:8069/test/result) on the browser to check if it is available, and I get function index at 0x7f04a28>, /test/result: Function declared as capable of handling request of type 'json' but called with a request of type 'http'
. This way I know that the controller is listening at that URL and is expecting JSON data.
因此,我打开一个Python控制台并输入:
So I open a Python console and type:
import json
import requests
data = {'test': 'Hello'}
data_json = json.dumps(data)
r = requests.get('http://localhost:8069/test/result', data=data_json)
当我在控制台中打印 r 时,它会返回< Response [404]> ,我在日志中看不到任何消息
When I print r in the console, it returns <Response [404]>, and I cannot see any message in the log (I was expecting The controller is called.).
这里有一个类似的问题,但是它并不完全一样:
There is a similar question here, but it is not exactly the same case:
OpenERP @ http.route('demo_json',type =" json)URL不显示JSON数据
任何人都可以帮助我?
Can anyone help me? What am I doing wrong?
推荐答案
我刚刚解决了这个问题。
I have just solved the problem.
首先,如@techsavvy所说,我不得不修改装饰器,写 type ='http'
而不是 type ='json'
。
Firstly, as @techsavvy told, I had to modify the decorator, to write type='http'
instead of type='json'
.
然后,控制台的请求返回了404错误,因为它不知道它发送数据到哪个数据库。在localhost:8069我有多个数据库。所以我试图在那个港口只有一个。也就是说,现在它工作的很好!
And after that, the request from the console returned a 404 error because it did not know which database it was sending data to. In localhost:8069 I had more than one database. So I tried to have only one at that port. And that is, now it works great!
为了管理而不删除任何其他数据库,我刚刚修改配置文件更改参数 db_filter
,并放入一个只包含我当前数据库的正则表达式。
To manage that without removing any of the other databases, I have just modified the config file to change the parameter db_filter
and put there a regular expression which only included my current database.
这篇关于如何在Odoo控制器中获取JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!