Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to return a code 400 http response from json type controller?

By
zbik
on 6/28/15, 10:35 AM 2,814 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





1

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 6/29/15, 2:00 PM

I found solution:

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

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 6/29/15, 3:53 AM

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

zbik
on 6/29/15, 9:45 AM

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

Temur
on 6/29/15, 9:53 AM

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

zbik
on 6/29/15, 10:08 AM

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

Temur
on 6/29/15, 10:54 AM

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

Temur
on 6/29/15, 11:20 AM

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!

zbik
on 6/29/15, 12:08 PM

I found solution,

zbik
on 6/29/15, 2:10 PM

congrats ;)

Temur
on 6/29/15, 2:12 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/28/15, 10:35 AM
Seen: 2814 times
Last updated: 6/29/15, 2:12 PM