Example of Python service from Post method

Asked

Viewed 226 times

0

Any example of a Python Post method service? I don’t 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)

1 answer

0


It’s actually something very similar to your example. You need to change the method to POST and if you expect content like application/json, the request object may be used 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 carry out queries in the database you can follow the way you have been working on the method that receives the GET.

  • And how does the database connection work? Have an example?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.