Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
4871 Visualizzazioni
​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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

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
Abbandona
Autore

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.

Post correlati Risposte Visualizzazioni Attività
1
mar 24
2361
4
mar 25
3576
0
lug 24
21
2
dic 23
19334
0
nov 22
2487