Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
7 Trả lời
49881 Lượt xem

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





Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

I found solution:

from openerp.http import Response 
if not result:
Response.status = "400 Bad Request" 
Ảnh đại diện
Huỷ bỏ

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

Câu trả lời hay nhất

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)

 


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
from odoo.http import Response
@http.route('/send', auth='none', type='json') def send(self, **kwargs): Response.status = '400' return {'test':True}
Ảnh đại diện
Huỷ bỏ

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

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

@Zbik, What was your solution?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

UPDATED

try:

import werkzeug

...

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả

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

Tác giả

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!

Tác giả

I found solution,

congrats ;)

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 10 20
3855
0
thg 2 21
3598
1
thg 1 20
9332
0
thg 4 17
3155
2
thg 2 23
3157