Zum Inhalt springen
Odoo Menü
  • Anmelden
  • Jetzt gratis testen
  • Apps
    Finanzen
    • Buchhaltung
    • Rechnungsstellung
    • Spesenabrechnung
    • Tabellenkalkulation (BI)
    • Dokumente
    • E-Signatur
    Vertrieb
    • CRM
    • Vertrieb
    • Kassensystem – Shop
    • Kassensystem – Restaurant
    • Abonnements
    • Vermietung
    Websites
    • Website-Builder
    • E-Commerce
    • Blog
    • Forum
    • Livechat
    • E-Learning
    Lieferkette
    • Lager
    • Fertigung
    • PLM
    • Einkauf
    • Wartung
    • Qualität
    Personalwesen
    • Mitarbeiter
    • Personalbeschaffung
    • Abwesenheiten
    • Mitarbeiterbeurteilung
    • Personalempfehlungen
    • Fuhrpark
    Marketing
    • Social Marketing
    • E-Mail-Marketing
    • SMS-Marketing
    • Veranstaltungen
    • Marketing-Automatisierung
    • Umfragen
    Dienstleistungen
    • Projekte
    • Zeiterfassung
    • Außendienst
    • Kundendienst
    • Planung
    • Termine
    Produktivität
    • Dialog
    • Genehmigungen
    • IoT
    • VoIP
    • Wissensdatenbank
    • WhatsApp
    Apps von Drittanbietern Odoo Studio Odoo Cloud-Plattform
  • Branchen
    Einzelhandel
    • Buchladen
    • Kleidergeschäft
    • Möbelhaus
    • Lebensmittelgeschäft
    • Baumarkt
    • Spielwarengeschäft
    Essen & Gastgewerbe
    • Bar und Kneipe
    • Restaurant
    • Fast Food
    • Gästehaus
    • Getränkehändler
    • Hotel
    Immobilien
    • Immobilienagentur
    • Architekturbüro
    • Baugewerbe
    • Immobilienverwaltung
    • Gartenarbeit
    • Eigentümervereinigung
    Beratung
    • Buchhaltungsfirma
    • Odoo-Partner
    • Marketingagentur
    • Anwaltskanzlei
    • Talentakquise
    • Prüfung & Zertifizierung
    Fertigung
    • Textil
    • Metall
    • Möbel
    • Speisen
    • Brauerei
    • Firmengeschenke
    Gesundheit & Fitness
    • Sportklub
    • Brillengeschäft
    • Fitnessstudio
    • Therapeut
    • Apotheke
    • Friseursalon
    Handel
    • Handyman
    • IT-Hardware & -Support
    • Solarenergiesysteme
    • Schuster
    • Reinigungsdienstleistungen
    • HLK-Dienstleistungen
    Sonstiges
    • Gemeinnützige Organisation
    • Umweltschutzagentur
    • Plakatwandvermietung
    • Fotostudio
    • Fahrrad-Leasing
    • Software-Händler
    Alle Branchen ansehen
  • Community
    Lernen
    • Tutorials
    • Dokumentation
    • Zertifizierungen
    • Schulung
    • Blog
    • Podcast
    Bildung fördern
    • Bildungsprogramm
    • Scale-Up! Planspiel
    • Odoo besuchen
    Software anfragen
    • Herunterladen
    • Editionen vergleichen
    • Releases
    Zusammenarbeiten
    • Github
    • Forum
    • Veranstaltungen
    • Übersetzungen
    • Partner werden
    • Dienstleistungen für Partner
    • Buchhaltungsfirma registrieren
    Services anfragen
    • Partner finden
    • Buchhalter finden
    • Einen Experten treffen
    • Implementierungsservices
    • Kundenreferenzen
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Eine Demo erhalten
  • Preiskalkulation
  • Hilfe

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Buchhaltung
  • Lager
  • PoS
  • Projekte
  • MRP
All apps
Sie müssen registriert sein, um mit der Community zu interagieren.
Alle Beiträge Personen Abzeichen
Stichwörter (Alle anzeigen)
odoo accounting v14 pos v15
Über dieses Forum
Sie müssen registriert sein, um mit der Community zu interagieren.
Alle Beiträge Personen Abzeichen
Stichwörter (Alle anzeigen)
odoo accounting v14 pos v15
Über dieses Forum
Hilfe

How to set response status code in controller with type json in odoo17

Abonnieren

Erhalten Sie eine Benachrichtigung, wenn es eine Aktivität zu diesem Beitrag gibt

Diese Frage wurde gekennzeichnet
controllersresponsejsonrpcv17
3 Antworten
7510 Ansichten
Avatar
Muhammad Mudasir
​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



0
Avatar
Verwerfen
Avatar
Kothiya Rajesh
Beste Antwort

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


0
Avatar
Verwerfen
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Beste Antwort

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

0
Avatar
Verwerfen
Avatar
Nikhil Nakrani
Beste Antwort

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

0
Avatar
Verwerfen
Muhammad Mudasir
Autor

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.

Diskutieren Sie gerne? Treten Sie bei, statt nur zu lesen!

Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!

Registrieren
Verknüpfte Beiträge Antworten Ansichten Aktivität
How to return/raise a different response than 200 from a controller call?
development controllers v17
Avatar
Avatar
1
März 24
3787
How to call controller method from Javascript Odoo17 Gelöst
javascript controllers JavascriptCallFromOdoo v17
Avatar
Avatar
Avatar
Avatar
Avatar
4
März 25
4948
Need want to display username per question who submit it!
username controllers v17 webform
Avatar
0
Juli 24
21
How to send json in response at Get Endpoint Odoo v11 Gelöst
json controllers jsonrpc odoov11
Avatar
Avatar
Avatar
2
Dez. 23
20844
how to change the json response format
api json controllers jsonrpc v15
Avatar
0
Nov. 22
3422
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Herunterladen
  • Github
  • Runbot
  • Übersetzungen
Dienstleistungen
  • Odoo.sh-Hosting
  • Support
  • Upgrade
  • Individuelle Entwicklungen
  • Bildung
  • Buchhalter finden
  • Partner finden
  • Partner werden
Über uns
  • Unsere Firma
  • Markenwerte
  • Kontakt
  • Karriere
  • Veranstaltungen
  • Podcast
  • Blog
  • Kunden
  • Rechtliches • Datenschutz
  • Sicherheit
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo ist eine Suite von Open-Source-Betriebsanwendungen, die alle Bedürfnisse Ihres Unternehmens abdecken: CRM, E-Commerce, Buchhaltung, Lager, Kassensystem, Projektmanagement etc.

Das einzigartige Wertversprechen von Odoo ist, dass es gleichzeitig sehr einfach zu bedienen und voll integriert ist.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now