Post method Python service example

0

Any examples of a Python method service? I do not know how to do this with the database connection. I know how to do 'Get' and I'll leave an example below.

    @route('/dadosBloqueios', method = "GET")
    def dadosBloqueios():
        response.content_type = 'application/json;charset=utf-8'
        cnx = mysql.connector.connect(host='*', database='*',
                              user='*', password='*')
        cnx_cursor1 = cnx.cursor(dictionary=True)

        dadosBloqueios = { } #lista de dados

####
        sql1 = "SELECT count(*) FROM DB.Utilizadores where Bloqueado = 1;"
        cnx_cursor1.execute(sql1)

        l = cnx_cursor1.fetchone()
        while l is not None:    #vai ler tudo ate o l ser vazio
            dadosBloqueios["Número total de utilizadores bloqueados"] =         l["count(*)"]
            l = cnx_cursor1.fetchone()

        cnx.close()
        return json.dumps(dadosBloqueios)
    
asked by anonymous 04.07.2018 / 17:53

1 answer

0

In fact, it is something very similar to your example. You need to change the method for POST, and if you wait for content of type application / json , you can use the request object to retrieve the information as follows:

from flask import jsonify, request

@app.route("/", methods=["POST"])
def insert_new():
    json_request = request.get_json() # Recupera o JSON enviado
    # Acessando propriedade json_request["id_time"]

    # Montando retorno da operação, caso seja necessário
    return jsonify({"status": "ok", "obj": obj})

Once inside the operation, to perform queries in the database you can follow the way you have worked on the method that receives the GET.

    
04.07.2018 / 18:28