2
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").

14
  • for one thing, you're not supposed to put spaces in urls. '%20' is used instead of spaces most of the time (stackoverflow uses hypens '-', as you can tell). So try 127.0.0.1/app-api?store="Play%20Store" Commented Apr 24, 2014 at 7:24
  • filter_by accepts **kwargs. So you can just comma separate key-value pairs, e.g. PLAY_STORE.query.filter_by(Store="Play Store", Category="Games"). Please avoid using capital letters in your variable names and read the pep8 Commented Apr 24, 2014 at 8:01
  • thank you !!PLAY_STORE.query.filter_by(Store="Play Store",Category="games") is working Commented Apr 24, 2014 at 9:55
  • Play%20Store is not working Commented Apr 24, 2014 at 9:56
  • What is 'Store' value??, Can add this print request.args.get('store') and share the value Commented Apr 24, 2014 at 10:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.