简体   繁体   中英

GET request with parameters FlasK/Rest API

I am trying to do a GET request which should print a specific row from my database depending on what arguments are set. The argument should be a name of a course and I want it to get all data from the selected course. It might be a bit easier to explain this as a SQL query. The query could look like this "SELECT * FROM courselist WHERE course='D0024E';" where "course".

I have managed to do a fetchall() and receive all rows from a specific table, but I have not managed to get the parameters working correctly so I can get information from a specific course.

from flask import Flask
from flask import render_template
import requests
from flask import request
from flask import jsonify
import mysql.connector

app = Flask(__name__)

mydb = mysql.connector.connect(user='Mille',
                          auth_plugin='mysql_native_password',
                          password='jagheter12',
                          host='localhost',
                          database='paraplyet')

@app.route('/')
def index():
    return render_template("index2.html")

@app.route('/courses', methods= ["GET"])
def getStudentInCourse():
    myCursor2 = mydb.cursor()
    query2 = ("SELECT * FROM paraplyet.kursinfo")
    myCursor2.execute(query2)
    myresult2 = myCursor2.fetchall()

    return jsonify(myresult2)

if __name__ == '__main__':
    app.run()

You need to update your route url to receive parameters

@app.route('/courses/<course_code>', methods= ["GET"])
def getStudentInCourse(course_code):

Then you can use this course_code to filter result.

Actually there are several points where your code(s) can fail, because establishing a correct front-end back-end chain in Flask is a little tricky (but worth it at the end). You have a counter part front-end HTML code where you start your request with the proper variable, like "course" in your example, which may looks like this:

<form action="/courses" method="post">
  <input>
  <button type="submit"></button>
</form>

Then Flask as back-end will get this variable( parameter ) as part of your query string as part of the URL string . You can retrieve this parameter in the form:

course = request.form.get('course')

To achieve it you have to add "POST" the view's methods, as it handles only "GET" -s as default.

@app.route('/courses', methods=["GET", "POST"])

Then you can use this variable as you want to complete your back-end operations:

query2 = ("SELECT * FROM courseInfo where courseCode = '" + course +  "';")

those results then you can pass it back to the front-end via:

return jsonify(myresult2)

Your python/flask code should be some like as follows:

from flask import Flask
from flask import render_template
import requests
from flask import request
from flask import jsonify
import mysql.connector

app = Flask(__name__)

mydb = mysql.connector.connect(user='Mille',
                          auth_plugin='mysql_native_password',
                          password='jagheter12',
                          host='localhost',
                          database='paraplyet')

@app.route('/')
def index():
    return render_template("index2.html")

@app.route('/courses', methods= ["GET", "POST"])
def getStudentInCourse():

    if request.method == "POST" and request.form.get('course') != '':

        myCursor2 = mydb.cursor()

        course = request.form.get('course')
        query2 = ("SELECT * FROM courseInfo where courseCode = '" + course +  "';")

        myresult2 = myCursor2.execute(query2)

        return jsonify(myresult2)

if __name__ == '__main__':
    app.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM