Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask get method which accepts a parameter : Missing 1 required positional argument [duplicate]

Tags:

python

flask

I have the following flask code

from flask import Flask, jsonify, request                                                                                                                                                                          
from webargs import fields                                                                                                                                                                                         
from sqlalchemy import create_engine, MetaData, Table, select                                                                                                                                                      
from flask_cors import CORS                                                                                                                                                                                                                                                                                                                                                                                                           
engine = create_engine('...')                                                                                                                                                                                                                                                                                                                                            
////                                                                                                                                                                                                                                                                                                                                                                                                  
app = Flask(__name__)                                                                                                                                                                                                                                                                                                                                                                                                                 
cors = CORS(app, resources={r"/list*": {"origins": "*"}})                                                                                                                                                                                                                                                                                                                                                                             

@app.route('/list-vessels', methods=['GET'])                                                                                                                                                                       
def list_vessels():                                                                                                                                                                                                    
vessel_list = []                                                                                                                                                                                                   
s = select([vesseldetail.c.Vessel])                                                                                                                                                                                
rp = connection.execute(s)                                                                                                                                                                                         
for row in rp:                                                                                                                                                                                                         
    vessel_list.extend(list(row))                                                                                                                                                                                  
return jsonify(vessel_list)                                                                                                                                                                                                                                                                                                                                                                                                       

@app.route('/list-keydates', methods=['GET'])                                                                                                          
def list_keydates(vesselname):                                                                                                                                                                                         
    vesselname = request.args['vesselname']      
    intervention_list = []                                                                                                                                                                                             
    keydate_list = []                                                                                                                                                                                                  
    s = select([keydate.c.Intervention, keydate.c.Date])                                                                                                                                                               
    s = s.where(keydate.c.VESSEL==vesselname)                                                                                                                                                                          
    rp = connection.execute(s)                                                                                                                                                                                         
    for row in rp:                                                                                                                                                                                                         
        intervention_list.append((row[0]))                                                                                                                                                                                 
        keydate_list.append((row[1]))                                                                                                                                                                                  
    return jsonify({"keydates": keydate_list, "intervention": intervention_list})

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

The second endpoint 'list-keydates' returns a TypeError: list_keydates() missing 1 required positional argument: 'vesselname' even when I pass a vesselname in the query string. What am I doing wrong?

like image 420
spheroid Avatar asked Oct 20 '25 15:10

spheroid


2 Answers

The argument vesselname is missing when defining the url, try the following:

...
@app.route('/list-keydates/<vesselname>', methods=['GET'])                                                                                                          
def list_keydates(vesselname):
    intervention_list = []                                                                                                                                                                                             
    keydate_list = []                                                                                                                                                                                                  
    s = select([keydate.c.Intervention, keydate.c.Date])                                                                                                                                                               
    s = s.where(keydate.c.VESSEL==vesselname)  
    ...

url should be http://.../list-keydates/name-of-the-vessel

This way you don't have to get the vesselname from request args, its value will be placed in the variable function.

Hope it suits you well

like image 200
Kenny Aires Avatar answered Oct 22 '25 05:10

Kenny Aires


you can also do this

when there is a query string in the URL

http://localhost:5000/list-keydates?vesselname=yourdata

@app.route('/list-keydates', methods=['GET'])                                                                                                          
def list_keydates():
    print(request.args['vesselname']                                                                                                                                                                        
    vesselname = request.args['vesselname']
like image 44
Sreevardhan Reddy Avatar answered Oct 22 '25 04:10

Sreevardhan Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!