This question has been flagged
7 Replies
38659 Views

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





Avatar
Discard
Author Best Answer

I found solution:

from openerp.http import Response 
if not result:
Response.status = "400 Bad Request" 
Avatar
Discard

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!!!

Best Answer

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)

 


Avatar
Discard
Best Answer
from odoo.http import Response
@http.route('/send', auth='none', type='json') def send(self, **kwargs): Response.status = '400' return {'test':True}
Avatar
Discard

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

Best Answer

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

Avatar
Discard
Best Answer

@Zbik, What was your solution?

Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

UPDATED

try:

import werkzeug

...

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

Avatar
Discard
Author

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

Author

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...

Author

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!

Author

I found solution,

congrats ;)