تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
7 الردود
49934 أدوات العرض

Controller:

@http.route('/my_module/xxx/', type='json', auth='none', methods=['POST']) 
def my_foo(self, **post)
   data = request.jsonrequest
result = my_verify(data)
if not result:
### HOW RETURN/EXCEPT '400 Bad Request' header ???????????
return result





الصورة الرمزية
إهمال
الكاتب أفضل إجابة

I found solution:

from openerp.http import Response 
if not result:
Response.status = "400 Bad Request" 
الصورة الرمزية
إهمال

This is not working. It stops all the internal odoo requests.

Please be aware of the age of this answer.

RIP zbik.

Yah Ermin. Even its almost 5 years since the answer but I faced the problem these days. which is not fixed.

This worked for me in Odoo 14. Thank you!!!

أفضل إجابة

Solutions above didn't work for me at all.

This worked:

from odoo.http import Response
...
@route('/send', type='json', auth='none', methods=['POST'])
   def receive(self, **post): 
        ...
        if error:
            return Response("[error text]", status=400)

 


الصورة الرمزية
إهمال
أفضل إجابة
from odoo.http import Response
@http.route('/send', auth='none', type='json') def send(self, **kwargs): Response.status = '400' return {'test':True}
الصورة الرمزية
إهمال

Thank you! Your & @Zbik 's solution worked for me in Odoo 14.

أفضل إجابة

This worked for me:

from werkzeug.exceptions import NotFound

# in your method when exception occurs
raise NotFound('error message')
# this will have a 404 status code on the final response

الصورة الرمزية
إهمال
أفضل إجابة

@Zbik, What was your solution?

الصورة الرمزية
إهمال
أفضل إجابة

I had the same problem. 

https://www.odoo.com/forum/help-1/can-not-set-response-status-code-in-route-with-type-json-in-odoo14-187781

الصورة الرمزية
إهمال
أفضل إجابة

UPDATED

try:

import werkzeug

...

if not result:
    raise werkzeug.exceptions.BadRequest("Optional msg...") 

الصورة الرمزية
إهمال
الكاتب

This solution generate: 'ERROR xxx openerp.http: Exception during JSON request handling.' ... Traceback (most recent call last): ....BadRequest: 400: Bad Request .... but .... sent header = 200

normally we should use "error" member in response of json-rpc in order to report abnormal situation to the caller. so in json-rpc normally header will set to 200, but "error" will indicate abnormal situation... see: http://www.jsonrpc.org/specification#response_object

الكاتب

Unfortunately, I have to generate a different code than 200, because it requires payment provider.

according a table in the following link a json-response with -32600 error code, say: {...,'error':{'code':-32600,'message':'Invalid Request.','data':'Some Data...'},...} should be transfered with 400 http header... see:
http://www.jsonrpc.org/historical/json-rpc-over-http.html#errors
try to generate response containing "error" with mentioned code and check if it will be transferred to the caller with 400 http header...

if you can't get it worked with controller type="json" then I suggest to revert to type="http" as an option... in this case you'll be done with error part, but you'll have to carry about generating correctly normal json resopnses...

الكاتب

Revert to type="http" impssible :(. Json response error code is hard coded in http.py, class JsonRequest, method __handle_exception. This is my BIG problem!

الكاتب

I found solution,

congrats ;)

المنشورات ذات الصلة الردود أدوات العرض النشاط
2
أكتوبر 20
3871
0
فبراير 21
3620
1
يناير 20
9353
0
أبريل 17
3184
2
فبراير 23
3181