JaviAguiler
JaviAguiler

Reputation: 11

Angular get request with param to Flask REST resource not working

I'm doing a get request from Angular to a Flask REST resource (without query params) and all works fine (request received and data show in the view). The problem is when I try to send some param to the Flask resource (using get), I receive from Flask the 'TypeError get() missing 1 required positional argument: 'startdate'. See below extracts of my source code:

ANGULAR (canvases.service.ts)

export class CanvasesService {
   
  constructor(private http:HttpClient) {}

  getDeliveredLastMonth(startdate: string):Observable<any> {
    
    startdate=startdate.trim();
     
    // Add safe, URL encoded search parameter if there is a search term
    const options = startdate ?
     { params: new HttpParams().set('StartDate', startdate) } : {};
    
    return this.http.get("http://127.0.0.1:5000/deliveredlastmonth", options)

  }

FLASK

class DeliveredLastMonth(Resource):
     
    def get(self,startdate):
        con = sqlite3.connect (r"C:\Users\Documents\Python\jiradata.db")
        cur=con.cursor()
        #Delivered from start date onwards 
        query="SELECT canvid,product,summary,status,resolveddate from CAnalysis where releasedate>='" +startdate+ "')"

        resultset=cur.execute(query).fetchall()
        data=pd.DataFrame(resultset)
        field_names = [i[0] for i in cur.description]
        data.columns=field_names

        return Response(data.to_json(orient="records"), mimetype="application/json")

.....
.....

api.add_resource(DeliveredLastMonth, '/deliveredlastmonth')  

Could you please help me troubleshoot why this is not working when adding a param to the get request? Again, it's working without param (getting all data from the static query), but after spending many hours browsing several tutorials I'm struggling to make it work when factoring in params (to make dynamic DB queries). Many thanks in advance!

Upvotes: 0

Views: 153

Answers (1)

faris404
faris404

Reputation: 373

Try this

class DeliveredLastMonth(Resource):
    # remove startdate from here
    def get(self):
        # get startdate from query string
        startdate = request.args.get("startdate")

        con = sqlite3.connect (r"C:\Users\Documents\Python\jiradata.db")
        cur=con.cursor()
        #Delivered from start date onwards 
        query="SELECT canvid,product,summary,status,resolveddate from CAnalysis where releasedate>='" +startdate+ "')"

        resultset=cur.execute(query).fetchall()
        data=pd.DataFrame(resultset)
        field_names = [i[0] for i in cur.description]
        data.columns=field_names

        return Response(data.to_json(orient="records"), mimetype="application/json")


api.add_resource(DeliveredLastMonth, '/deliveredlastmonth') 

Upvotes: 1

Related Questions