Question

import config
from flask import Flask, jsonify, request
## app and db is defined on model.py
from model import db, app, PLAY_STORE
@app.route('/app-api', methods=['GET'])
def listapp():
    if request.method == 'GET':
        store=request.args.get('store')
        category=request.args.get('category')
        results = PLAY_STORE.query.filter_by(Store=store,Category=category).all()
        json_results = []
        for result in results:
          d = {'rank': result.Rank,
           'store': result.Store,
           'category': result.Category,
           'type': result.Type,
           'title': result.Title}
          json_results.append(d)

    return jsonify(items=json_results)

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

I am trying to build a rest api on top of mysql database using flask. In the above program I am trying to get the store and category from the url. Even though I use request.args.get, Store value is empty,i.e json_results is empty.If I hard code making PLAY_STORE.query.filter_by(Store="Play Store") i am getting the expected output.But when i type 127.0.0.1/app-api?store="Play Store" json_results is empty.

I have one more issue .How to put multiple conditions in the filter_by, something like PLAY_STORE.query.filter_by(Store="Play Store" and Category="Games").

Was it helpful?

Solution

Problem is with your GET url parameters

Try this url:

  127.0.0.1:5000/app-api?store=Play Store&category=Games

You don't need to send single quotes in querystring values i.e

 <your-host>/?store=XXXXXX not like this <your-host>/?store='XXXXXX'

Also, filter_by is used for simple queries, If you want to make complex Queries use filter and for more info go through this StackOverflow question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top