I write a odoo program in python, this way request type text/plain is working. And application/json request work in any other odoo version(Odoo 15,Odoo 16) .But odoo 13 application/json request not working.
My odoo python controller code here:-
class CamsAttendance(http.Controller):
@http.route('/sample/sample_json/', method=["POST"], csrf=False, auth='public', type="http")
def generate_attendance(self, **params):
data = json.loads(request.httprequest.data)
json_object = json.dumps(data)
sample = json.loads(json_object)
print(sample['sample_code'])
return "process competed" //example
view the image on request and response data via postman:-
Request application/json data:- (sample request)
{
"sample_code":"sample_code"
}
Response data:-
{
"jsonrpc": "2.0",
"id": null,
"error": {
"code": 200,
"message": "Odoo Server Error",
"data": {
"name": "werkzeug.exceptions.BadRequest",
"debug": "Traceback (most recent call last):\n File \"C:\\odoo 13\\Odoo 13.0\\server\\odoo\\http.py\", line 624, in _handle_exception\n return super(JsonRequest, self)._handle_exception(exception)\n File \"C:\\odoo 13\\Odoo 13.0\\server\\odoo\\http.py\", line 310, in _handle_exception\n raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])\n File \"C:\\odoo 13\\Odoo 13.0\\server\\odoo\\tools\\pycompat.py\", line 14, in reraise\n raise value\n File \"C:\\odoo 13\\Odoo 13.0\\server\\odoo\\http.py\", line 669, in dispatch\n result = self._call_function(**self.params)\n File \"C:\\odoo 13\\Odoo 13.0\\server\\odoo\\http.py\", line 318, in _call_function\n raise werkzeug.exceptions.BadRequest(msg % params)\nwerkzeug.exceptions.BadRequest: 400 Bad Request: <function CamsAttendance.generate_attendance at 0x0644C930>, /cams/biometric-api3.0: Function declared as capable of handling request of type 'http' but called with a request of type 'json'\n",
"message": "400 Bad Request: <function CamsAttendance.generate_attendance at 0x0644C930>, /cams/biometric-api3.0: Function declared as capable of handling request of type 'http' but called with a request of type 'json'",
"arguments": [],
"exception_type": "internal_error",
"context": {}
}
}
}
Another way try :- I note the error ** 'http' but called with a request of type 'json'"** in the response and i change the code contoroller code on @http.route('/sample/sample_json/', method=["POST"], csrf=False, auth='public', type="json")
But,
Response:-
{
"jsonrpc": "2.0",
"id": null,
"result": "process competed"
}
** but i want :-** "process competed
Mainly this problem Odoo 13 only. And i what process the code and expecting text message the response
CodePudding user response:
Have you tried to change the type
of the controller route to json
?
Also see the official documentation
odoo.http.route(route=None, **kw)
Decorator marking the decorated method as being a handler for requests. The method must be part of a subclass of Controller.
Parameters
route – string or array. The route part that will determine which http requests will match the decorated method. Can be a single string or an array of strings. See werkzeug’s routing documentation for the format of route expression ( http://werkzeug.pocoo.org/docs/routing/ ).
type – The type of request, can be 'http' or 'json'.
auth –
The type of authentication method, can on of the following:
user: The user must be authenticated and the current request will perform using the rights of the user.
public: The user may or may not be authenticated. If she isn’t, the current request will perform using the shared Public user.
none: The method is always active, even if there is no database. Mainly used by the framework and authentication modules. There request code will not have any facilities to access the database nor have any configuration indicating the current database nor the current user.
methods – A sequence of http methods this route applies to. If not specified, all methods are allowed.
cors – The Access-Control-Allow-Origin cors directive value.
csrf (bool) –
Whether CSRF protection should be enabled for the route.
Defaults to True. See CSRF Protection for more.
CodePudding user response:
As CZoellner said, you need to change type
to json
.
You also need to include OPTIONS
in request methods and enable cors for the request.
@http.route('/sample/sample_json/', type='json', auth='public', cors='*', csrf=False, methods=['POST', 'OPTIONS'])
Ensure your request body follows odoo's syntax
{
jsonrpc: 2.0,
params: {
sample_code: "sample_code"
}
}