[Python] Ensure the POST data is valid JSON

Discussão em 'Python' iniciado por Stack, Abril 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am developping a JSON API with Python Flask.
    What I want is to always return JSON, with a error message indicating any error that occured.

    That API also only accept JSON data in the POST body, but Flask by default return a HTML error 400 if it can't read the data as JSON.

    Preferably, I d also like to not force the user to send the Content-Type header, and if raw or text content-type, try to parse the body as JSON nonetheless.

    In short, I need a way to validate that the POST body's is JSON, and handle the error myself.

    I've read about adding decorator to request to do that, but no comprehensive example.

    Continue reading...

Compartilhe esta Página