Skip to Content
Menu
This question has been flagged
3 Replies
3763 Views
​if not vendor: 
​​response=request.make_json_response(data={'message': 'Invalid ​vendor'},
​headers={"Content-Type": "application/json"},status=401)
​return response

I want to send different status code for different conditions but i get only 200 for every condition 

but the response is like 

{    "jsonrpc": "2.0",    "id": null,    "result": ""} with status 200 

so how to change response status in odoo 17



Avatar
Discard
Best Answer

Hii Mudasir,

I encountered the same issue with setting the response status code in APIs using type='json'. To solve it, I switched my controller to type='http', which worked perfectly.

While "Cybrosys Techno Solutions Pvt.Ltd" offers a reasonable solution, it only sets the status code inside the result, not for the entire API response. Other approaches also involve some additional customizations.

The solution is simple: you can set the status code and return any response you need by using type='http' and returning request.make_response.


Take a look at the example code below:


@http.route('/api/custom_endpoint', type='http', auth='public', methods=['GET'])
def custom_endpoint(self):
    """
    A simple example of an Odoo GET API endpoint that returns a custom status code.
    :return: JSON response with status code.
    """
    # Example response data
    data = {
        'message': 'Hello, this is a custom response!',
    }
    # Convert the response data to JSON format
    data = json.dumps(data)
    # Create an HTTP response with status code 200 (OK)
    response = request.make_response(
        data,
        headers=[
            ('Content-Type', 'application/json'),
            ('Cache-Control', 'no-cache'),
        ],
        status=200  # You can change this to any status code you need
    )
    return response


Avatar
Discard
Best Answer

Hi,

from odoo import http
from odoo.http import request
from werkzeug.wrappers import Response
import json

class YourController(http.Controller):

    @http.route('/your/route', type='json', auth='public', methods=['POST'], csrf=False)
    def your_method(self, **post):
        # Your logic to check vendor
        if not vendor:
            # Create JSON response with status code 401
            response = Response(json.dumps({'message': 'Invalid vendor'}), status=401, content_type='application/json')
            return response
Here we are importing Response from werkzeug.wrappers to get the response status.
It allow creating custom responses as shown!


Hope it helps

Avatar
Discard
Best Answer

Hi Mudasir,

Can you check this may be help,

https://stackoverflow.com/questions/67646804/how-to-set-response-status-code-in-route-with-type-json-in-odoo-14.

Thanks

Avatar
Discard
Author

Thank you for your response, but this solution does not work for me. I want a solution for Odoo 17. In Odoo 14, there was a class called JsonResponse, but in Odoo 17, this class does not exist.

Related Posts Replies Views Activity
1
Mar 24
2003
4
Mar 25
3179
0
Jul 24
21
2
Dec 23
18944
0
Nov 22
2242